Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to retrieve information from a JSON-structure in Javascript I had this first:

<script type="text/javascript">
    var jsonfile= '{"products": 
                       {"productInfo1": "test",
                        "productInfo2": "test" }
                   }'; 

    var digitalData = JSON.parse(jsonfile);
    console.log(digtialData.products.productInfo1); 
    console.log(digtialData.products.productInfo2); 
</script>

I get the output "test" in my console window. This worked fine. But it seems that I need to declare new objects, for proper use. As I understand, they don't like this solution. The method described above would be bad . So I tried to create a new script.

<script type="text/javascript">
    var digitalData = new digitalData();
    var product = new digitalData.product();
    var productInfo1 = new digitalData.product.productInfo1();
    var productInfo1 = new digitalData.product.productInfo2();
    productInfo1 = "test";
    productInfo2 = "test";
    console.debug(digitalData.product.productInfo);
    console.debug(digitalData.product.productInfo);
</script>

Then I get the error: "Uncaught TypeError: undefined is not a function index.php? route=product/product&path=57&product_id=49:265 (anonymous function)"

I tried various variants, and abused Google to find an answer how get this work. I found some answers, like to get digitalData.product working. But I'm looking to get the 3rd object working, and link that in any way to 1/2 objects (digitalData and product).

Anyone have suggestions how to solve this?

share|improve this question
1  
Have you tried something like var digitalData = {}; instead? –  SLoW Feb 14 at 14:36
1  
you need also need to post digital data function to seconds script the be more clear. console.debug ing only the digitalData instead of last 2 console.debugs in second script may help you see what you are doing wrong –  Quad Feb 14 at 14:37
    
You need a function called digitalData before you can do new digitalData() to create it. Same for product and productInfo –  Matt Burland Feb 14 at 14:43
    
var digitalData = new digitalData(); overrides the original constructor. Use other variable name. –  Shuhei Kagawa Feb 14 at 14:43

3 Answers 3

up vote 1 down vote accepted

Not quite sure why you had a problem with the first approach. But to use the second approach, you need to define you objects before you can instantiate them with new

 function DigitalData() {

 }

 var digitalData = new DigitalData();

Then for product you could do something like this:

 function Product() {

 }

 digitalData.product = new Product();

But it might be easier to actually instantiate that in the constructor for DigitalData because otherwise, what's the point of the constructor?

 function DigitalData() {
      var self = this;
      self.product = new Product();    
 }

 var digitalData = new DigitalData();
 var product = digitalData.product;

Now the productInfo1 and 2 part sounds like it ought to be an array. So you could do something like this:

 function ProductInfo(name) {
      var self = this;
      self.name = name;
 }

 function Product(arrayofNames) {
      var self = this;
      self.productInfo = arrayofNames.map(function(n) { new ProductInfo(n); });
 }

 function DigitalData(arrayofNames) {
     var self = this;
     self.product = new Product(arrayOfNames);
 }

 var digitalData = new DigitalData({ "product1","product2" });

 console.log(digitalData.product.productInfo[0].name);   // logs "product1"

But if you don't want to use the array, then:

 function Product() {
      var self = this;
      self.productInfo1 = new ProductInfo();
      self.productInfo2 = new ProductInfo();
 }
share|improve this answer
    
Your reply worked too. But as I see, the other reply of the user Uttam is shorter, and don't use functions. Much less code, better, I guess? But I planned to study the objects in JS deeper. Thanks for your reply anyway! :) –  Julius Martin Feb 14 at 14:57

firstly new keyword is just for functions, in javascript. then if you do:

var digitalData = new digitalData();

It means you have a function in your code and you want to create a object based on that.

Second you can get all your inner objects in your object like this:

digtialData.products
digtialData["products"]

and for the rest:

digtialData.products.productInfo1
digtialData["products"].productInfo1
digtialData["products"]["productInfo1"]

you can also create a class like function, like:

DigitalData = function DigitalData(){
    this.products = [];
};
DigitalData.prototype.addProduct = function(info1, info2){
    var product = new DigitalData.Product();
    product.productInfo1 = info1;
    product.productInfo2 = info2;
    this.products.push(product);
};

DigitalData.Product = function Product(info1, info2){
    this.productInfo1 = info1;
    this.productInfo2 = info2;
};
var digitalData = new DigitalData();
digitalData.addProduct("test1", "test2");
digitalData.addProduct("test11", "test22");

var jsonString = JSON.stringify(digitalData);

console.log(jsonString);
share|improve this answer
    
Hmmz. I don't understand it. But I planned to study the objects in JS deeper. Thanks for your reply anyway! :) –  Julius Martin Feb 14 at 14:56

Try this

var digitalData = new Object();
var products = new Object();
products.productInfo1 = "test";
products.productInfo2 = "test";

digitalData.products = products;

console.debug(digitalData.products.productInfo1);
console.debug(digitalData.products.productInfo2);
share|improve this answer
    
Your solution was the working answer! –  Julius Martin Feb 14 at 14:54
    
Cool.. If the answer satisfied your requirements, you can accept the answer.. –  Uttam Feb 14 at 15:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.