// 会改变原数组
const nestedArray = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
const flattenedArray = nestedArray.flatMap((arr) => arr.pop());
console.log(flattenedArray);
// 输出: [3, 6, 9]
// 不会改变原数组,这个方式比较推荐
const nestedArray = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
const oneDimArray = nestedArray.map((subArray) => subArray.slice(-1)[0]);
console.log(oneDimArray);
// 输出: [3, 6, 9]
二维数组,转换为一维数组,并且只保留最后一级
于 2023-06-26 13:52:06 首次发布