2017-08-01 67 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);