Home > Mobile >  javacsript array map with double [[ ]] data
javacsript array map with double [[ ]] data

Time:12-22

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.

CodePudding user response:

This should work:

arrr = arrr[0][0]

CodePudding user response:

If your data is in an array arr, there are a couple of ways:

  1. Just do arr[0].map(...) (if your data looks like in your example)
  2. Use flatMap. If there is nesting like [ [ obj1, obj2 ], [ obj3, obj4 ] ] flatMap turns it to [obj1, obj2, obj3, obj4]

Example:

arr.flatMap(_ => _)
  • Related