我正在尝试映射嵌套数组并返回字母数超过6的单词,我已经卡住了这个问题一段时间所以我想得到一些帮助
const array = [["hellow",'pastas'],["travel", "militarie"],["oranges","mint"]]
const arrayOne = array.map(new => new).filter(arr =>arr.length > 6)
你可以先flat
数组,而不是filter
长度大于6
的单词
const array = [['hellow','pastas'],['travel', 'militaries'],['oranges','mint']]
const arrayOne = array.flat(1).filter(e=> e.length > 6 )
console.log(arrayOne)
您可以使用以下代码。此代码使用.map()
和.filter()
来检查长度是否大于6,如果是,则将其添加到数组中。
const array = [["hellow","pastas"],["travel", "militarie"],["oranges","mint"]];
const arrayOne = array.map(e1 => e1.filter(e2 => e2.length > 6)).flat();
console.log(arrayOne);
我认为更好的是filter()方法。
array.filter(function (c) {
return c.length < 6;
});
但首先使用flat()方法。
有很多方法可以做到这一点。
const array = [['hellow','pastas'],['travel', 'militarie'],['oranges','mint']];
const result = array.flatMap(x => x.filter(y => y.length > 6));
console.log(result);
const array = [['hellow','pastas'],['travel', 'militarie'],['oranges','mint']];
const result = array.reduce((acc, x) => [...acc, ...x.filter(y => y.length > 6)], []);
console.log(result);