我在获取.filter()方法以筛选正确的索引时遇到麻烦

是编码新手,需要标题为filter()的函数的帮助。 filter假定从indexNum数组中获取所有其他元素(从索引1开始,不包括最后一个索引)。 filter应该使用这3个索引处的值(应为79、113、154),使用这3个值作为BetterWords数组的索引位置并过滤掉它们,从而创建一个名为finishStatement的新数组。 请提供任何帮助。

我会尽力减少代码,但我觉得需要提供上下文。

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 storyWords = story.split(' ');
let overusedWords = ['really','very','basically'];
let unnecessaryWords = ['extremely','literally','actually' ];
const betterWords = storyWords.filter(word =>{
  return !unnecessaryWords.includes(word);
})
var indexNum = [];
var id = 0;
for(i of overusedWords){
  id =+ 1;
  const correction = () => {
    var iP = betterWords.indexOf(i);
    while(iP != -1){
      indexNum.push(iP);
      iP = betterWords.indexOf(i,iP + 1);
    }
  }
  correction();
}
var finishedStatement = [];
const filter = () => {
  for (i = 1; i < indexNum.length-1; i = i + 2){
     console.log(indexNum[i]);
     console.log(betterWords[indexNum[i]]);
     finishedStatement = betterWords.filter(word => word != betterWords[indexNum[i]]);
  }
};    
filter()

所以我再一次没有做的是,从indexNum数组的索引1开始,使用这三个值(79、113、154)将所有其他值(不包括最后一个索引)用作索引号for BetterWords数组以将其过滤掉。这应该创建一个名为finishStatement的新数组。因此,finishStatement ===(更好的词-(索引79、113、154))。

请帮助!

sixupiaofubud 回答:我在获取.filter()方法以筛选正确的索引时遇到麻烦

据我了解,您想从故事中删除不必要的单词吗?您想使用故事中单词的索引来过滤它。我所做的是将过滤后的索引存储到数组中,然后通过存储的索引过滤故事。如果我对问题的理解不正确,请纠正我。

 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 storyWords = story.split(' ');
    let overusedWords = ['really','very','basically'];
    let unnecessaryWords = ['extremely','literally','actually' ];
    const betterWords = storyWords.filter(word =>{
      return !unnecessaryWords.includes(word);
    })
    var indexNum = [];
    var id = 0;
    for(i of overusedWords){
      id =+ 1;
      const correction = () => {
        var iP = betterWords.indexOf(i);
        while(iP != -1){
          indexNum.push(iP);
          iP = betterWords.indexOf(i,iP + 1);
        }
      }
      correction();
    }
    var finishedStatement = [];
    var fIndex = [];
    
    const filter = () => {
      for (i = 1; i < indexNum.length-1; i = i + 2){
         fIndex.push(indexNum[i]);
         console.log(indexNum[i],betterWords[indexNum[i]]);
         finishedStatement = betterWords.filter((word,i) => !fIndex.filter((idx) => idx == i).length > 0);
      }
    };    
    filter()
    
    console.log(finishedStatement.join(' '))

本文链接:https://www.f2er.com/3169424.html

大家都在问