Home > Software engineering >  How to merge a multiple array structure from one array into one group of arrays? (JS)
How to merge a multiple array structure from one array into one group of arrays? (JS)

Time:03-15

I'm sure this is a dumb question but I'm really struggling with the syntax for this. In javascript I have this:

  const arr = [
[
  {
    test: "hey1",
  },
  
],
[
  {
    test: "hey2",
  },
  
],

]

This prints out like this:

 Array(2)
0: [{…}]
1: [{…}]

So accessing it right now is like this arr[0][1].test where i just want to be able to access the other arrays in the array like arr[1].test

Hope that makes sense and once again sorry for the dumb question!

CodePudding user response:

Just delete the internal square brackets just like this

[
  {Test: 1},
  {Test: 2}
]

CodePudding user response:

If I understood you correctly you want to unite all the arrays you have into one array

open a new array variable

allArr=[]

And run in a loop on the old array by using Spread Operato

for (let i = 0; i < oldArr.length; i  ) {
    allArr.push([...i])

    //OR
    
    allArr= [...allArr,...i]
    
}


  • Related