将对象数组的数组转换为数组的数组
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:我使用的是另一个 post 的 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);
我必须遵循以下数组:
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:我使用的是另一个 post 的 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);