[
[ { id:1 } , { id:2 } ],
[ { id:3 } , { id:4 } ],
[ { id:5 } , { id:6 } ],
]
arr.map((item, index) => item.map(ii => {
return [ii.id, index]
}));
// output
[
[ [1, 0], [2, 0] ],
[ [3, 1], [4, 1] ],
[ [5, 2], [6, 2] ]
]
arr.flatMap((item, index) => item.map(ii => {
return [ii.id, index]
}));
// output
[
[1, 0], [2, 0],
[3, 1], [4, 1],
[5, 2], [6, 2]
]
// new Map() Yes!😆
0: {1 => 0}
1: {2 => 0}
2: {3 => 1}
3: {4 => 1}
4: {5 => 2}
5: {6 => 2}
数组扁平
var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
Array.prototype.transfer = function (d) {
return d > 0 ?
this.reduce((acc, val) =>
acc.concat(Array.isArray(val) ? Array.prototype.transfer.call(val, d - 1) : val), []
): this.slice();
};
console.log(arr1.transfer(1));
// > Array [1, 2, 3, 1, 2, 3, 4, Array [2, 3, 4]]