[英]Convert Array of arrays of objects to Array of Arrays
我必須遵循數組:
var arr1 = [
[{n:"0.1",m:"m.0",other:"eg1"}],
[{n:"1.1",m:"m.1",other:"eg2"}],
[{n:"2.1",m:"m.2",other:"eg3"}]
];
我想將其轉換為數組數組,如下所示:
var arr1 = [
["0.1","0"],
["1.1","1"],
["2.1","2"]
];
我只想將幾個屬性轉換為另一個數組,而不是全部。 知道我該怎么做嗎?
PS:我在另一篇文章中使用flatMap
但它不起作用,因為它不存在於 Edge 中。
下次你必須嘗試。
這是您問題的解決方案
var arr1 = [
[{n:"0.1",m:"m.0",other:"eg1"}],
[{n:"1.1",m:"m.1",other:"eg2"}],
[{n:"2.1",m:"m.2",other:"eg3"}]
];
arr1 = arr1.map(currentArray=>{
const item = currentArray.shift();
return [item.n,item.m.replace( /^\D+/g, '')]
});
假設每個子數組中的第二個值來自m
鍵的句點之后的數字,您可以使用 map 函數來完成此操作:
var arr1 = [ [{n:"0.1",m:"m.0",other:"eg1"}], [{n:"1.1",m:"m.1",other:"eg2"}], [{n:"2.1",m:"m.2",other:"eg3"}] ]; var newArray = arr1.map(x => [x[0].n, x[0].m.split('.')[1]]); console.log(newArray);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.