I have this array:
[ [ { "data1": 1, "data2": 2, }, { "data1": 1, "data2": 2, } ] ]
how can i remove the first parenthesis? i tried with reduce or map but return an error:
Cannot read properties of undefined (reading 'reduce') or Cannot read properties of undefined (reading 'map').
I have to retrieve some data inside it.
Thanks for your help.
you can use array.flat() here
let x =[ [ { "data1": 1, "data2": 2, }, { "data1": 1, "data2": 2, } ] ]
let y = x.flat()
console.log(y) // [ { data1: 1, data2: 2 }, { data1: 1, data2: 2 } ]
If your data is in an array arr, there are a couple of ways:
arr[0].map(...) (if your data looks like in your example)[ [ obj1, obj2 ], [ obj3, obj4 ] ] flatMap turns it to [obj1, obj2, obj3, obj4]Example:
arr.flatMap(_ => _)
Edit: As others have mentioned, flat works just as well. It depends whether you want to perform some operations within the flatMap also, or to get an array of only the data1 properties of the objects in your example:
arr.flatMap(el => el["data1"])