JSON搜索循环在第一次命中后停止



d问候,亲爱的stackoverflow社区!我的问题似乎很容易,但是我只是在做错的地方不知道。

所以,我有一个带有以下数据的JSON:

var data = [{
  "id": 0,
  "friends": ["Mike", "John"],
  "enemies": ["Albert", "Amy"],
  "image": "https://link.com/1"
}, {
  "id": 1,
  "friends": ["Chad", "John"],
  "enemies": ["Lawrence", "Amy"],
  "image": "https://link.com/2"
}, {
  "id": 2,
  "friends": ["Craig", "John"],
  "enemies": ["Adam", "Amy"],
  "image": "https://link.com/3"
}, {
  "id": 3,
  "friends": ["Craig", "Bruce"],
  "enemies": ["Adam", "Scott"],
  "image": "https://link.com/4"
}];

现在,我正在尝试使用用户的输入来循环此数据。例如,如果用户在" adam"中类型类型,我想获得对象的ID,在敌人数组中呈现可怜的亚当。

到目前为止,我提出了以下代码:

function getObjectByEnemyName(name){
  for (var i = 0; i < data.length; i++) {               // In each object of the data array
    for (var m = 0; m < data[i].enemies.length; m++) { // Locate the enemies array and search through each of its elements
      if (data[i].enemies[m] == name) {              // If the m'th element of the enemies array in the i'th object of the data array equals to the entered string
        return data[i].id                                  // Return the id value of the data array's i'th object
      }
    }
  }
}

var found = getObjectByEnemyName("Adam");

,如果我要搜索,例如"阿尔伯特",这完全可以,因为他只在enemies数组中呈现一次。

但是,当涉及到" adam"之类的查询时,我的功能在数据数组的第三个对象(ID = 2)中找到了第一个正确的命中,并且拒绝继续进行,而输出为2,当下一个对象中实际上有另一个" adam",我希望结果是这样的东西:

['2', '3']

为了实现这种行为,我尝试在我使用的功能中插入更多的forwhile循环,但失败了。我还尝试使用一些第三方节点软件包来搜索我的数据阵列,但也没有成功。

因此,我的问题是:有没有办法告诉我的循环继续寻找其他比赛而不停止第一次正确的命中?任何帮助将不胜感激。

p.s。:我本人控制了JSON数据的外观,因此,如果问题隐藏在其组合方式中,我可以轻松更改它,请让我知道如何。

非常感谢您的友好关注!

实际上它在第一场比赛中停止,因为您正在返回第一场比赛...

如果您想要Muliple匹配,则可以将它们存储在数组中,并在搜索完成后返回整个数组:

function getObjectByEnemyName(name) {
  var results = [];
  for (var i = 0; i < data.length; i++) { // In each object of the data array
    for (var m = 0; m < data[i].enemies.length; m++) { // Locate the enemies array and search through each of its elements
      if (data[i].enemies[m] == name) { // If the m'th element of the enemies array in the i'th object of the data array equals to the entered string
        results.push(data[i].id); // Return the id value of the data array's i'th object
      }
    }
  }
  return results;
}

顺便说一句,您可以按以下方式简化搜索:

function getObjectsByEnemyName(name) {
     return data.filter(item => item.enemies.includes(name)).map(item => item.id);
}

使用 Array#filter获取物品,该项目带有您搜索的敌人(在这种情况下为'adam'),然后 Array#map将它们 id

var data = [{"id": 0,"friends": ["Mike", "John"],"enemies": ["Albert", "Amy"],"image": "https://link.com/1"}, {"id": 1,"friends": ["Chad", "John"],"enemies": ["Lawrence", "Amy"],"image": "https://link.com/2"}, {"id": 2,"friends": ["Craig", "John"],"enemies": ["Adam", "Amy"],"image": "https://link.com/3"}, {"id": 3,"friends": ["Craig", "Bruce"],"enemies": ["Adam", "Scott"],"image": "https://link.com/4"}]
var search = "Adam";
var result = data.filter(function(item) {
  return item.enemies.indexOf(search) !== -1;
}).map(function(item) {
  return item.id;
});
console.log(result);

您也可以使用Array#reduce在一次通过中进行操作:

var data = [{"id": 0,"friends": ["Mike", "John"],"enemies": ["Albert", "Amy"],"image": "https://link.com/1"}, {"id": 1,"friends": ["Chad", "John"],"enemies": ["Lawrence", "Amy"],"image": "https://link.com/2"}, {"id": 2,"friends": ["Craig", "John"],"enemies": ["Adam", "Amy"],"image": "https://link.com/3"}, {"id": 3,"friends": ["Craig", "Bruce"],"enemies": ["Adam", "Scott"],"image": "https://link.com/4"}]
var search = "Adam";
var result = data.reduce(function(arr, item) {
  item.enemies.indexOf(search) === -1 || arr.push(item.id);
  
  return arr;
}, []);
console.log(result);

最新更新