[英]How to remove second square brackets from an array using JavaScript?
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 [{}]
.我希望将其从
[[{}]]
更改为[{}]
。
You can use Array.flat()
to flatten sub-arrays:您可以使用
Array.flat()
来展平子数组:
const data = [ [ {id: 1}, {id: 2} ], [ {id: 3}, {id: 4} ] ]; const newData = data.flat(); console.log(newData);
If Array.flat()
is not supported, you can spread the array into Array.concat()
:如果不支持
Array.flat()
,您可以将数组扩展到Array.concat()
:
const data = [ [ {id: 1}, {id: 2} ], [ {id: 3}, {id: 4} ] ]; const newData = [].concat(...data); console.log(newData);
Extract first item of your array提取数组的第一项
var data = [ [ {id: 1}, {id: 2} ] ] console.log(data); var newData = data[0]; console.log(newData);
What about If i have 那如果我有
[[{},{},{}]] [[{},{},{}]]
I want to remove extra brackets outside and want this [{},{},{}] 我想在外面删除多余的括号,并希望此[{},{},{}]
I am not able to clean it up the array. 我无法清理阵列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.