2017-08-01 65 views
1

我试图将句子从一个地方推到另一个地方。搜索对象和.push到新位置

这一切都取决于找到哪些关键字。如果一个句子有两个关键字,那么它会被推送到第一个关键字。

  • 猫 - 推猫在他们所有的句子keywordsFound.cats
  • 狗 - 推动与狗在他们所有的句子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

+0

您同时在'search'功能您正在寻找的'如果(长期== '仓鼠')调用'搜索( 'hamst')' {'。您必须更正测试或通话以使其正常工作。 –

+0

为了改善你的代码,使用'switch'而不是几个'if'。阅读和表现更好。 –

+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);

+0

根据最终用户输入的搜索输入“term”,使用JavaScript switch switches'。不要认为这是一个好主意,因为您可以通过Object .keys(userInput.keywordsFound).find(k => k。包括(术语))'检查我的回答 –

+1

是的,如果输入数据来自用户,使用'switch'不是最好的选择,但它可以在这个特定的用例中起作用。感谢您指出这一点。无论如何,你的解决方案在那里,如果有人需要它,可以使用它。 – codtex

1

尝试使用ES6箭头函数String.prototype.includes()和阵列函数Array.prototype.forEach()Array.prototype.find()来确定正确的key变量推送:userInput.keywordsFound[key].push(str)

代码:

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