javascript 从JS数据结构中去除一层嵌套数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15678355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:39:55  来源:igfitidea点击:

Remove one level of nested arrays from JS data structure

javascript

提问by Psl

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.

我只想删除数组。

回答by Sam

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];

回答by Ankit Tyagi

Try this :

试试这个 :

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