【问题标题】:.filter and .includes to filter array with array?.filter 和 .includes 用数组过滤数组?
【发布时间】:2017-12-13 04:43:53
【问题描述】:

我对 javascript 还是很陌生,并且通过学习课程来获得一些经验,但有时我会在返回概念上打破我的头脑。基本上这是我坚持的任务:

有一个词数组是不必要的。遍历您的数组以过滤掉这些单词。将剩余的单词保存在一个名为 betterWords 的数组中。您可以通过多种方式实现这一目标。

我尝试了很多不同的东西......但我无法让它发挥作用。

代码:

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';

let overusedWords = ['really', 'very', 'basically'];
let unnecessaryWords = ['extremely', 'literally', 'actually' ];
let storyWords = story.split(' ');
console.log(storyWords.length);

let betterWords = storyWords.filter(function(words){
  if(story.includes(unnecessaryWords) === false){
    return words;
  }
});
console.log(betterWords);

我知道你们可能会发现你们眼中愚蠢的错误,但我来这里是为了从我的错误中学习,并最终将这个该死的退货概念带入我的大脑(我对简单的退货没有任何问题,但是当它嵌套时我很困惑哈哈)

【问题讨论】:

  • 你不应该调用javascript“java”,因为java是一种完全不同的编程语言......:/
  • 你需要在你的代码中清楚地告诉你哪个数组用于
  • 如果您只想包含unnecessaryWords 中不存在的字词,请使用function(word) { return !unnecessaryWords.includes(word); } 过滤。
  • Iterate over your array 哪个数组?

标签: javascript arrays filter


【解决方案1】:

您应该检查当前单词是否包含在数组unnecessaryWords中:

let betterWords = storyWords.filter(function(currentWord) {
    return !unnecessaryWords.includes(currentWord);
});

filter 将循环数组storyWords,并且对于该数组的每个单词currentWord,它将调用一个函数,如果该函数返回true,那么currentWord 将包含在结果数组中,否则不会。现在,对于每个currentWord,我们要检查unnecessaryWords 是否包含它,如果包含它,我们将返回false,如果不包含,我们将返回true。运算符!用于反转布尔值(如果includes返回true我们想返回false,如果它返回false我们想返回true,所以我们只使用!反转includes 的结果并返回)。

【讨论】:

  • 我正要写同样的东西:p
【解决方案2】:

在这个练习中推荐使用.filter()和.includes()方法,重要的是说“不包括”(注意!符号)

let betterWords = storyWords.filter(word => { return !unnecessaryWords.includes(word); })

然后您可以打印结果以查看它是否有效:

console.log(betterWords.join(' '));

【讨论】:

    【解决方案3】:

    filter 函数希望您在回调中返回 true 或 false。 要检查某个项目是否存在于数组中,请使用indexOf。 除非您打算重新分配变量,否则您不需要使用 let。

    结果:

    let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';
    
    const overusedWords = ['really', 'very', 'basically'];
    const unnecessaryWords = ['extremely', 'literally', 'actually' ];
    const storyWords = story.split(' ');
    console.log(storyWords.length);
    
    const betterWords = storyWords.filter(function(word){
      return unnecessaryWords.indexOf(word) < 0;
    });
    console.log(betterWords);

    【讨论】:

    • @klugojo
    • indexOf 是一个整数,表示数组中元素的索引。如果未找到该元素,则它等于-1。结论,unnecessaryWords.indexOf(word) &lt; 0 表示word 内部不存在unnecessaryWords
    【解决方案4】:

    您可以执行以下操作,这些操作一目了然,易于阅读和理解。另一个好处是,如果不必要的单词确实不是动态的,那么不必要的单词的长度很明显,如果将来您在审查/编辑/等您的代码时可能会派上用场

    let betterWords = storyWords.filter( (v) =>
      (
        v !== unnecessaryWords[0] &&
        v !== unnecessaryWords[1] &&
        v !== unnecessaryWords[2]
      )
    )
    

    或者如果不必要的Words 数组是动态的...

    let betterWords = storyWords.filter( (v) =>
      return !unnecessaryWords.includes(v)
    )
    

    【讨论】:

    • 为什么要检查unnecessaryWords 的每个元素?你可以使用storyWords.filter((v)=&gt;unnecesaryWords.indexOf(v) == -1)
    猜你喜欢
    • 2020-11-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 2020-12-11
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多