0

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

3
  • 1
    your data is not valid with nested objects without property. Commented Feb 3, 2021 at 19:09
  • I attached the console log data into my question @NinaScholz Commented Feb 3, 2021 at 19:13
  • 1
    So you have objects with properties 0, 1, ... etc., which are not arrays? Nice. Commented Feb 3, 2021 at 19:14

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.