Amin Azimi
Amin Azimi

Reputation: 749

merged array objects in javascript

I have an array that includes of objects and every objects includes of objects, I want convert below array

[  
  { 
    0: { a: 1, b: 2 }, 
    1: { c: 3, d: 4 } 
  },
  { 
    0: { e: 5, f: 6 }, 
    1: { g: 7, h: 8 },
    2: { i: 9, j: 10 },
  } 
]

to this array

[  
  { a: 1, b: 2 }, 
  { c: 3, d: 4 }, 
  { e: 5, f: 6 }, 
  { g: 7, h: 8 },
  { i: 9, j: 10 }
]

enter image description here

Upvotes: 0

Views: 49

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386881

You could get a flat array with assigning the objects to an array.

const
    data = [{ 0: { a: 1, b: 2 }, 1: { c: 3, d: 4 } }, { 0: { e: 5, f: 6 }, 1: { g: 7, h: 8 } }],
    flat = data.flatMap(o => Object.assign([], o));

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

Upvotes: 4

Related Questions