'How to convert nested object into an array in javascript?

I have an array with multiple objects. In this array each objects having two or more sub objects. I want to get together all sub objects into an array of data. How to do with javascript?

var array1 = [
  {
    "dfgasg24":{
      name:"a",
      id:1
    },
    "dfgare24":{
      name:"b",
      id:2
    }
  },
  {
    "wegasg24":{
      name:"ab",
      id:76
    },
    "yugasg24":{
      name:"bc",
      id:34
    },
    "yugasg26":{
      name:"dc",
      id:45
    }
  }
]

The output which i want to be like this,

var result = [
    {
        name:"a",
        id:1
    },
    {
        name:"b",
        id:2
    },
    {
        name:"ab",
        id:76
    },
    {
        name:"bc",
        id:34
    },
    {
        name:"dc",
        id:45
    }
];


Solution 1:[1]

You could use a combined approach with iterating over the array and over the keys for building a flat array.

var array = [{ "dfgasg24": { name: "a", id: 1 }, "dfgare24": { name: "b", id: 2 } }, { "wegasg24": { name: "ab", id: 76 }, "yugasg24": { name: "bc", id: 34 }, "yugasg26": { name: "dc", id: 45 } }],
    result = array.reduce(function (r, o) {
        Object.keys(o).forEach(function (k) {
            r.push(o[k]);
        });
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Solution 2:[2]

Use Array.flatMap() with Object.values():

const array = [{ "dfgasg24": { name: "a", id: 1 }, "dfgare24": { name: "b", id: 2 } }, { "wegasg24": { name: "ab", id: 76 }, "yugasg24": { name: "bc", id: 34 }, "yugasg26": { name: "dc", id: 45 } }];

const result = array.flatMap(Object.values);

console.log(result);

If Array.flatMap() is not supported, use Array.map() with Object.values(), and flatten the results by spreading into Array.concat():

const array = [{ "dfgasg24": { name: "a", id: 1 }, "dfgare24": { name: "b", id: 2 } }, { "wegasg24": { name: "ab", id: 76 }, "yugasg24": { name: "bc", id: 34 }, "yugasg26": { name: "dc", id: 45 } }];

const result = [].concat(...array.map(Object.values));

console.log(result);

If all objects' keys are unique, you also use Object.assign() to combine all objects to a single one, then extract the values to an array with Object.values():

const array = [{ "dfgasg24": { name: "a", id: 1 }, "dfgare24": { name: "b", id: 2 } }, { "wegasg24": { name: "ab", id: 76 }, "yugasg24": { name: "bc", id: 34 }, "yugasg26": { name: "dc", id: 45 } }];

const result = Object.values(Object.assign({}, ...array));

console.log(result);

Solution 3:[3]

use _.values to get object values

var res = _.flatMap(array1, _.values)

Solution 4:[4]

answer=[];
for(elem of array){
var arr=[];
 for(obj of elem){
   arr.push(obj);
 }
answer.push(arr);
}
alert(answer);

Loop trough the main array, and replace each elem with an array.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nina Scholz
Solution 2
Solution 3 stasovlas
Solution 4 Jonas Wilms