2 回答
TA贡献1765条经验 获得超5个赞
你已经提到了解决方案,你只需要实现它 - 当前项到回调内部的累加器:concatreduce
const arrays = [["how", "now"], ["brown", "cow"]];
const flattenedArray = arrays.reduce((a,c) => a.concat(c));
console.log(flattenedArray);
但会容易得多:.flat()
const arrays = [["how", "now"], ["brown", "cow"]];
const flattenedArray = arrays.flat();
console.log(flattenedArray);
TA贡献1804条经验 获得超3个赞
另一个选项 - 平面图:
const arrays = [["how", "now"], ["brown", "cow"]];
const flattenedArray = arrays.flatMap(a => a);
console.log(flattenedArray);
但是,只有当你想在地图中做一些事情时,它才是真正有利的,就像这样:
const arrays = [["how", "now"], ["brown", "cow"]];
const flattenedArray = arrays.flatMap(a => a.concat(a));
console.log(flattenedArray);
或者像这样:
const arrays = [["how", "now"], ["brown", "cow"]];
const flattenedArray = arrays.flatMap(a => a.map(b => b.toUpperCase()));
console.log(flattenedArray);
添加回答
举报