Home > Software engineering >  Extracting properties from nested objects and arrays
Extracting properties from nested objects and arrays

Time:02-26

This one may have been answered in the past, but I seem to be having trouble iterating through a nested structure and cannot find anything that will point me in the right direction. Below is the object I am trying to parse.

const nestedArray = 
  { id       : 100
  , interval : 1000
  , enable   : true
  , topics: 
    [ { topic1: 
        [ { id: 0, 'error-code' : 100 } 
        , { id: 1, status       : 200 } 
        , { id: 2, mode         : 300 } 
      ] } 
    , { topic2: 
        [ { id: 0, count     : 100 } 
        , { id: 1, total     : 200 } 
        , { id: 2, operation : 300 } 
  ] } ] }

I want to iterate nestedArray and from topics extract the the topic name i.e."topic1" and the key (name) in the associated key-value properties in the nested array i.e. "id" and "error-code".

I am new to this and have tried Object.entries, Object.keys, Object.values and a recursive function etc but never get the values I need or error because the method cant be applied to the Object. I would appreciate some explanation regarding how this is achieved.

CodePudding user response:

const nestedArray = 
  { id       : 100,
    interval : 1000,
    enable   : true,
    topics: 
    [ { topic1: 
        [ { id: 0, errorcode : 100 },
          { id: 1, status     : 200 },
          { id: 2, mode       : 300 } 
        ]
      },
      { topic2: 
        [ { id: 0, count     : 100 },
          { id: 1, total     : 200 },
          { id: 2, operation : 300 } 
        ]
      }
    ]
  };
  
  nestedArray.topics.forEach((topic)=>{
   for(let obj in topic){
   console.log(obj);
     topic[obj].forEach((key)=>{
       for(let insideKey in key){
         console.log(insideKey, key[insideKey]);
       }
     });
   }
  });

nestedArray is an object so I can directly reference the topics array using nestedArray.topics

arrays have a forEach method which I call using nestedArray.topics.forEach(); this iterates through the array of topics objects

I then create a loop for each obj in the array I can get the 'topic1' value (keyName) just be referencing the obj ....

since obj now represents another array of objects, we can use the forEach method again....and well pass each object into the function in the variable called key.....key now represents your innerMost object i.e. {id: 0, errorcode: 100}

so now we get each key inside that object and log the key and the key value to the console.....

CodePudding user response:

Something like that ?

const nestedArray = 
  { id       : 100
  , interval : 1000
  , enable   : true
  , topics: 
    [ { topic1: 
        [ { id: 0, 'error-code' : 100 } 
        , { id: 1, status       : 200 } 
        , { id: 2, mode         : 300 } 
      ] } 
    , { topic2: 
        [ { id: 0, count     : 100 } 
        , { id: 1, total     : 200 } 
        , { id: 2, operation : 300 } 
  ] } ] }

nestedArray.topics.forEach( topic =>
  {
  let key0 = Object.keys( topic)[0]
  topic[key0].forEach(el =>
    console.log( key0, '->', Object.keys(el). join(', ')))
  })

console.log( '\notherwise :')

nestedArray.topics.forEach( topic =>
  {
  let key0 = Object.keys( topic)[0]
  topic[key0].forEach(el =>
    console.log( key0, '->', Object.entries(el).map(([k,v])=>`${k}: ${v} `).join(', ')))
  })
.as-console-wrapper {max-height: 100%!important;top:0 }
.as-console-row::after { display:none !important; }

  • Related