📅  最后修改于: 2023-12-03 15:16:18.151000             🧑  作者: Mango
在JavaScript中,数组是一个非常常见的数据结构。而flatMap()是一个在ES2019中新增的函数, 它可以将一个数组的每个元素都转化为一个新的数组,然后将所有新的子数组(或者他们的元素)合并为一个新的数组。
arr.flatMap(callback(currentValue[, index[, array]])[, thisArg])
其中:
const arr1 = [1, 2, 3, 4];
const arr2 = arr1.flatMap(x => [x * 2]);
console.log(arr2);
// Output: [2, 4, 6, 8]
const arr3 = [10, 11, 12];
const arr4 = arr1.flatMap(x => [x, x + 1], arr3);
console.log(arr4);
// Output: [1, 2, 2, 3, 3, 4, 4, 5, 10, 11, 12]
上面的例子中,我们可以看到:
以上就是有关JavaScript数组flatMap()函数的介绍。使用flatMap()函数可以帮助我们更便捷、高效地操作数组,提高开发效率。