0

I want to access nested JSON object values by iterating.

I tried console.log(test[0].Invoice[0].Cost.NO[0]); but it's not working.

var test = [{
  "Invoice": {
    "NO": "869",
    "$$hashKey": "object:186"
  },
  "Cost": [{
    "NO": 183,
    "Amnt": 100
  }, {
    "NO": 184,
    "Amnt": 200
  }]
}, {
  "Invoice": {
    "NO": "698",
    "$$hashKey": "object:189"
  },
  "Cost": [{
    "NO": 110,
    "Amnt": 150
  }, {
    "NO": 142,
    "Amnt": 263
  }]
}];

console.log(test[0].Invoice[0].Cost.NO[0]);

3
  • I tried this. console.log(test[0].Invoice[0].Cost.NO[0]);
    – Khan
    Commented Mar 30, 2017 at 11:36
  • add this to your question
    – hering
    Commented Mar 30, 2017 at 11:36
  • i see no JSON! Commented Mar 30, 2017 at 11:54

5 Answers 5

1

Your code console.log(test[0].Invoice[0].Cost.NO[0]); won't work.

Because Invoice and Cost are same level objects (Cost isn't nested in Invoice).

Check below example on how to access them.

You would need to understand Arrays [] and Objects {}.

var test = [{
  "Invoice": {
    "NO": "869",
    "$$hashKey": "object:186"
  },
  "Cost": [{
    "NO": 183,
    "Amnt": 100
  }, {
    "NO": 184,
    "Amnt": 200
  }]
}, {
  "Invoice": {
    "NO": "698",
    "$$hashKey": "object:189"
  },
  "Cost": [{
    "NO": 110,
    "Amnt": 150
  }, {
    "NO": 142,
    "Amnt": 263
  }]
}];

// To access COST
console.log(test[0].Cost[0].NO);

// To access Invoice
console.log(test[0].Invoice.NO);

0

This is your JSON:

var test = [{
  "Invoice": {
    "NO": "869"
  },
  "Cost": [{
    "NO": 183,
    "Amnt": 100
  }, {
    "NO": 184,
    "Amnt": 200
  }]
}, {
  "Invoice": {
    "NO": "698"
  },
  "Cost": [{
    "NO": 110,
    "Amnt": 150
  }, {
    "NO": 142,
    "Amnt": 263
  }]
}];

console.log(test[0].Cost[0].NO); would be 183

You can check your structure in development with console.log(test) if you are not used to JSON.

To iterate you should use a loop as described here: https://www.w3schools.com/jsref/jsref_forEach.asp

Short example:

 var myFunction = function(item,index){
    console.log(item.Cost[0].No);
 }
 test.forEach(myFunction)
0

test is an array thus you need to iterate over it.

Each iteration will give you an object with 2 properties : Invoice and Cost

Invoice is again an object thus you only need to access the property you need

Cost is an array thus you need to iterate each items which are objects

var test = [{
  "Invoice": {
    "NO": "869",
    "$$hashKey": "object:186"
  },
  "Cost": [{
    "NO": 183,
    "Amnt": 100
  }, {
    "NO": 184,
    "Amnt": 200
  }]
}, {
  "Invoice": {
    "NO": "698",
    "$$hashKey": "object:189"
  },
  "Cost": [{
    "NO": 110,
    "Amnt": 150
  }, {
    "NO": 142,
    "Amnt": 263
  }]
}];

test.forEach(t=>{
  console.log("Invoice N°: " + t.Invoice.NO);
  t.Cost.forEach(c=>{
    console.log("- Cost N°: "+ c.NO + " - Amount: " + c.Amnt);
  });
});

0

You have written wrong statement

test[0].Invoice[0].Cost.NO[0]

It should be as below

 test[0].Cost[0].NO

Because Invoice and Cost are properties of same json object.

try out this

  var test = [{ "Invoice": { "NO": "869", "$$hashKey": "object:186" }, "Cost": [{ "NO": 183, "Amnt": 100 }, { "NO": 184, "Amnt": 200 }] }, { "Invoice": { "NO": "698", "$$hashKey": "object:189" }, "Cost": [{ "NO": 110, "Amnt": 150 }, { "NO": 142, "Amnt": 263 }] }];
 

  console.log(test[0].Cost[0]);
 console.log(test[0].Cost[0].NO);
   

 

0
 var test = [{ "Invoice": { "NO": "869", "$$hashKey": "object:186" }, "Cost": [{ "NO": 183, "Amnt": 100 }, { "NO": 184, "Amnt": 200 }] }, { "Invoice": { "NO": "698", "$$hashKey": "object:189" }, "Cost": [{ "NO": 110, "Amnt": 150 }, { "NO": 142, "Amnt": 263 }] }];
        for (var t in test)
        {
            var obj = test[t];
            console.log(obj.Invoice.NO);
            console.log(obj.Invoice.$$hashKey);
            for ( var cst in obj.Cost)
            {
                console.log("Cost Value: " + obj.Cost[cst].NO + " " + obj.Cost[cst].Amnt);
            }
        }

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