搜索对象和.push到新位置

时间:2017-08-01 08:03:53

标签: javascript jquery

我试图将句子从一个地方推到另一个地方。

这完全取决于找到的关键字。如果一个句子有两个关键词,那么它会被推到第一个关键词。

  • cat - 将所有带有cat的句子推送到 keywordsFound.cats
  • dog - 将所有带有狗的句子推送到 keywordsFound.dogs
  • hamst - 将所有带有仓鼠的句子推送到 keywordsFound.hamsters



var data = "I have an oriental cat. I have both a dog and a cat. I always takes care of my American shorthair cat. I have a Birman cat. My parents bought me two Golden Retriever dogs. My dog is a German Shepherd. I always wanted a hamster. I don't have a pet."

var userInput = {
  all: [
    "I have an oriental cat.",
    "I have both a dog and a cat.", //Should only push the first one - dog
    "I always take care of my American shorthair cat.",
    "I have a Birman cat.",
    "My parents bought me two Golden Retriever dogs.",
    "My dog is a German Shepherd.",
    "I always wanted a hamster.",
    "I don't have a pet."
  ],
  keywordsFound: {
    cats: [/*Push all sentences with cat here*/],
    dogs: [/*Push all sentences with dog here*/],
    hamsters: [/*Push all sentences with hamster here*/]
  },
  noKeywordsFound: [],
}

function search(term) {
  if (userInput.all.indexOf(term) !== -1) {
    if (term == 'cat') {
     }
    if (term == 'dog') {
     }
    if (term == 'hamster') {
    }

  } 
  else {
    //Push to noKeywordsFound as an array
  }
}

//Which ever comes first gets pushed
search('cat')
search('dog') //Contains
search('hamster') //Contains




2 个答案:

答案 0 :(得分:1)

好的,所以你使用indexOf走在正确的轨道上,但是你在错误的地方使用它(userInput.all.indexOf(term)):)

那么你应该在search()函数中做的是:

1)遍历userInput.all的每个元素 - 您可以定期for循环或使用Array.prototype.reduce()来获取所有匹配的句子。

2)现在在循环内部对每个元素使用indexOf来检查它是否包含搜索词

3)如果找到术语,则插入所需的集合

所以我会稍微改变你的代码:



var userInput = {
  all: [
    "I have an oriental cat.",
    "I have both a dog and a cat.", //Should only push the first one - dog
    "I always take care of my American shorthair cat.",
    "I have a Birman cat.",
    "My parents bought me two Golden Retriever dogs.",
    "My dog is a German Shepherd.",
    "I always wanted a hamster.",
    "I don't have a pet."
  ],
  keywordsFound: {
    cats: [],
    dogs: [],
    hamsters: []
  },
  noKeywordsFound: [],
}

function search(term) {
  
  // Here you can do a basic for loop, but I'm using reduce function
  var foundItems = null;
  foundItems = userInput.all.reduce(function(found, current){
    if(~current.indexOf(term)){
      found.push(current);
    }
    return found;
  }, []);
  
  switch(term){
    case 'cat':{
      userInput.keywordsFound.cats = foundItems;
    }break;
    
    case 'dog':{
      userInput.keywordsFound.dogs = foundItems;
    }break;
    
    case 'hamster':{
      userInput.keywordsFound.hamsters = foundItems;
    }break;
  }

}

search('cat');
console.log('sentence with cats')
console.log(userInput.keywordsFound.cats);
search('dog'); 
console.log('sentence with dogs')
console.log(userInput.keywordsFound.dogs);
search('hamster'); 
console.log('sentence with hamsters')
console.log(userInput.keywordsFound.hamsters);




答案 1 :(得分:1)

尝试使用ES6箭头函数,String.prototype.includes()以及数组函数Array.prototype.forEach()Array.prototype.find()来确定要推送的key变量:userInput.keywordsFound[key].push(str)。< / p>

代码:

const userInput = {
    all: ["I have an oriental cat.", "I have both a dog and a cat.", "I always take care of my American shorthair cat.", "I have a Birman cat.", "My parents bought me two Golden Retriever dogs.", "My dog is a German Shepherd.", "I always wanted a hamster.", "I don't have a pet."],
    keywordsFound: {
      cats: [],
      dogs: [],
      hamsters: []
    },
    noKeywordsFound: [],
  },
  search = term => {
    userInput.all.forEach(str => {
      const key = Object.keys(userInput.keywordsFound).find(k => k.includes(term));
      key && str.includes(term) && userInput.keywordsFound[key].push(str);
    });
  };

//Which ever comes first gets pushed
search('cat');
search('dog'); //Contains dog
search('hams'); //Contains hamst

console.log(userInput.keywordsFound);