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]); Commented Mar 30, 2017 at 11:36
  • add this to your question 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);

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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);
  });
});

Comments

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);
   

 

Comments

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);
            }
        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.