Sathya
Sathya

Reputation: 1734

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

Upvotes: 1

Views: 4812

Answers (4)

Ori Drori
Ori Drori

Reputation: 191976

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

Upvotes: 4

stasovlas
stasovlas

Reputation: 7406

use _.values to get object values

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

Upvotes: 3

Jonas Wilms
Jonas Wilms

Reputation: 138257

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.

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386560

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

Upvotes: 7

Related Questions