簡體   English   中英

.filter 和 .includes 用數組過濾數組?

[英].filter and .includes to filter array with array?

我對 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);

我知道你們可能會發現在你們眼中很愚蠢的錯誤,但我在這里從我的錯誤中吸取教訓,並最終將這個該死的返回概念輸入我的大腦(我對簡單的返回沒有問題,但是當它嵌套時我很困惑,哈哈)

您應該檢查當前單詞是否包含在數組unnecessaryWords

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

filter將循環數組storyWords ,並且對於該數組的每個單詞currentWord ,它將調用一個函數,如果該函數返回truecurrentWord將包含在結果數組中,否則不會。 現在,對於每個currentWord ,我們要檢查unnecessaryWords包含它,如果包含它,我們將返回false ,如果沒有,我們將返回true 運營商! 用於反轉布爾值(如果includes返回true我們要返回false ,如果它返回false我們要返回true ,所以我們只需使用!來反轉includes的結果並返回)。

在這個練習中推薦使用.filter().includes()方法,重要的是說“不包括”(注意!符號)

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

然后您可以打印結果以查看它的工作情況:

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

filter函數希望您在回調中返回truefalse 要檢查數組中是否存在項目,請使用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);

您可以執行以下操作,這些操作可以輕松閱讀和一目了然。 另一個好處是,如果不必要的詞確實不是動態的,那么不必要的詞的長度是顯而易見的,如果將來您查看/編輯/等代碼時,這可能會派上用場

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

或者如果不必要的詞數組是動態的......

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM