Skip to content Skip to sidebar Skip to footer

Access Array Value Inside A Nested Object In Javascript

I believe there are few types of JSON objects we work with in daily life: Object with key value properties: { 'key' : 'value', 'key' : 'value', 'key' : 'value' } O

Solution 1:

!(function(){
  //data always like this:
  // this just an array 
  var json_array = [
    {id:1, name:"alex", age: 11},
    {id:2, name:"bob", age: 12},
    {id:3, name:"nn", age: 13},
  ];
 
  // page total_page don't need loop, direct use it
  var json_object = {
    page: 1,
    total_page: 20,
    total_item: 300,
    list: [
      {id:1, name:"alex", age: 11},
      {id:2, name:"bob", age: 12},
      {id:3, name:"nn", age: 13},
    ]
  }
  // or   for(var id in list_object){ console.log(list_object[id]['name'])  }
  var list_object = {
    1: {id:1, name:"alex", age: 11},
    2: {id:2, name:"bob", age: 12},
    3: {id:3, name:"nn", age: 13},
  }
  
  // your data need some change
  var data = {                    
      "key" : "value",
      "key" : "value",        
      "key1" : {

          "key11" : [
              {"key" : "value"},
              {"key" : "value"}
          ],

          "key12" : [
              {"key" : "value"},
              {"key" : "value"}
          ],
          "key13" : [
              {"key" : "value"},
              {"key" : "value"}
          ],
      }
  }
  
  // loop object
  for(var i in data['key1']){
    // loop array
    data['key1'][i].map(function(item, i){
      console.log(item['key'])
    })
  }

})()

Post a Comment for "Access Array Value Inside A Nested Object In Javascript"