映射并过滤数组JavaScript

问题描述 投票:-1回答:4

我正在尝试映射嵌套数组并返回字母数超过6的单词,我已经卡住了这个问题一段时间所以我想得到一些帮助

const array = [["hellow",'pastas'],["travel", "militarie"],["oranges","mint"]]

  const arrayOne = array.map(new => new).filter(arr =>arr.length > 6)
javascript arrays dictionary multidimensional-array filter
4个回答
0
投票

你可以先flat数组,而不是filter长度大于6的单词

const array = [['hellow','pastas'],['travel', 'militaries'],['oranges','mint']]

const arrayOne = array.flat(1).filter(e=> e.length > 6 )

console.log(arrayOne)

0
投票

您可以使用以下代码。此代码使用.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);

0
投票

我认为更好的是filter()方法。

array.filter(function (c) {
    return c.length < 6;
});

但首先使用flat()方法。


0
投票

有很多方法可以做到这一点。

你可以使用flatMapfilter

const array = [['hellow','pastas'],['travel', 'militarie'],['oranges','mint']];

const result = array.flatMap(x => x.filter(y => y.length > 6));

console.log(result);

另一种方法是使用reducefilter

const array = [['hellow','pastas'],['travel', 'militarie'],['oranges','mint']];

const result = array.reduce((acc, x) => [...acc, ...x.filter(y => y.length > 6)], []);

console.log(result);
© www.soinside.com 2019 - 2024. All rights reserved.