[Solved] How to parse JSON values inside nested array using JavaScript [closed]


There are many ways to iterate/loop through an array. One common way it using a for loop. There can be many variations of a for loop:

  • standard for loop (loops through numbers where i is each number):

    for(let i = 0; i < dangle.length; i++) { // loop through the dangle array
      let arr = dangle[i]; // get each inner array from index `i`
      for(let j = 0; j < arr.length; j++) { // loop through the inner array
        console.log(arr[j].name); // print the name of each object
      }
    }
    
let dangle =  [[{name:"jack",age:"20"}], [{name:"ram",age:"25"}],[{name:"vishy",age:"45"}]];
        
for (let i = 0; i < dangle.length; i++) {
  let arr = dangle[i];
  for (let j = 0; j < arr.length; j++) {
    console.log(arr[j].name);
  }
}
  • for of loop (loops through the array elements, where arr is the element):

    for(let arr of dangle) { // get each inner array in dangle (current inner array is referenced as arr)
      for(let obj of arr) { // loop through the contents in the current inner array, where obj is the contents
        console.log(obj.name); // print the name of the contents
      }
    }
    
let dangle =  [[{name:"jack",age:"20"}],[{name:"ram",age:"25"}],[{name:"vishy",age:"45"}]];
         
for(let arr of dangle) {
  for(let obj of arr) {
    console.log(obj.name);
  }
}
  • .forEach high-order function (loops through array elements, where arr is the element in each array)

    dangle.forEach(arr => arr.forEach(obj => {
      console.log(obj.name);
    }));
    
let dangle =  [[{name:"jack",age:"20"}],[{name:"ram",age:"25"}], [{name:"vishy",age:"45"}]];         

dangle.forEach(arr => arr.forEach(obj => { // loop through the outer array to access each inner array, loop through each inner array
  console.log(obj.name); // print the objects name retrieved from the inner array
}));

Notice: In the above examples, I’ve used 2 loops. The reason for this is because you have a 2-dimensional array (an array within another array). This means the outer for loop is used to access each inner array. The inner for loop then loops through the elements (objects in your case) in each inner array. As you only have one object in each inner array, the inner for loop can be removed and you can access the 0th index of each inner array. However, for the purposes of extendability and maintainability, I’ve used an inner for loop such that if you do have more than one object this will still work

Thus, you can use one of these for loop to populate an empty array with your names:

let names = [];
for(let arr of dangle) {
  for(let obj of arr) {
    names.push(obj.name); // add the name to the names array
  }
}

console.log(names);

let dangle=  [[{name:"jack",age:"20"}], [{name:"ram",age:"25"}], [{name:"vishy",age:"45"}]]

let names = [];
for(let arr of dangle) {
  for(let obj of arr) {
    names.push(obj.name); // add the name to the names array
  }
}

console.log(names);

0

solved How to parse JSON values inside nested array using JavaScript [closed]