映射和过滤数组JavaScript

时间:2019-03-16 18:28:05

标签: javascript arrays dictionary multidimensional-array filter

我正在尝试映射一个嵌套数组,并返回字母数大于6的单词,由于这个问题,我已经停留了一段时间,所以我想寻求帮助

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

  const arrayOne = array.map(new => new).filter(arr =>arr.length > 6)

4 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

我认为最好是使用filter()方法。

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

但是首先使用flat()方法。

答案 2 :(得分:0)

您可以使用下面的代码。此代码使用.map().filter()检查长度是否大于6,如果长度大于6,则将其添加到数组中。

const array = [["hellow","pastas"],["travel", "militarie"],["oranges","mint"]];
const arrayOne = array.map(e1 => e1.filter(e2 => e2.length > 6)).flat();

console.log(arrayOne);

答案 3 :(得分: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);