Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove one level of nested arrays from JS data structure

Tags:

javascript

How can I convert the following data structure:

var data = [ [ { time: 1, speed : 20 } ] ]; 

to

var data = [ { time: 1, speed: 54 } ];

I just want to remove the array.

like image 838
Psl Avatar asked Nov 30 '22 01:11

Psl


1 Answers

As the data is an array, you just want to select the first element of the outer array

so the solution would be

var data = [[{time:1,speed:20}]]; // Or whatever the data is
data = data[0];

Or if you're accessing the data via another object

var data = yourObject[0];
like image 199
Sam Avatar answered Dec 14 '22 11:12

Sam