gpt4 book ai didi

javascript - 搜索对象并 .push 到新位置

转载 作者:行者123 更新时间:2023-11-30 15:08:21 25 4
gpt4 key购买 nike

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

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

  • cat - 将所有包含 cat 的句子推送到 keywordsFound.cats
  • dog - 将所有包含 dog 的句子推送到 keywordsFound.dogs
  • hamst - 将所有包含 hamster 的句子推送到 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

最佳答案

好的,所以您使用 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);

关于javascript - 搜索对象并 .push 到新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45432223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com