How to remove second square brackets from an array using JavaScript?

167
March 08, 2018, at 6:02 PM

I have an array something like this:

data: [
        [
          {},{} ... //multiple objects
        ]
      ]

How do I remove those second square brackets? I want it to be changed from [[{}]] to [{}].

Answer 1

If you have an array with multiple sub arrays, you can spread the array into Array.concat():

const data = [ 
  [ 
    {id: 1}, 
    {id: 2} 
  ], 
  [ 
    {id: 3}, 
    {id: 4} 
  ] 
]; 
 
const newData = [].concat(...data); 
console.log(newData);

Answer 2

Extract first item of your array

var data = [ 
  [ 
    {id: 1}, 
    {id: 2} 
  ] 
] 
console.log(data); 
 
var newData = data[0]; 
console.log(newData);

READ ALSO
Find out If user is loged in (Laravel) in frontend

Find out If user is loged in (Laravel) in frontend

so I come to you with a simple question: how is laravel session relay working

135
Dashing-JS “Hello World” Demo

Dashing-JS “Hello World” Demo

I am attempting to work with Dashing-JS, a port of a Sinatra based framework project in Ruby, DashingIO, to Javascript/Node

148
Using continue as button for facebook login

Using continue as button for facebook login

I am building a React Native app, I previously implemented Facebook login using login Manager

165
How to achieve the following search function in html?

How to achieve the following search function in html?

picture 1 enter image description here

141