Skip to content Skip to sidebar Skip to footer

Remove One Level Of Nested Arrays From Js Data Structure

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.

Solution 1:

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

so the solution would be

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

Or if you're accessing the data via another object

vardata = yourObject[0];

Solution 2:

Try this :

JSON.stringify(data).substr(1,JSON.stringify(data).length-2);

Post a Comment for "Remove One Level Of Nested Arrays From Js Data Structure"