首次点击后,JSON搜索循环停止

时间:2016-12-11 22:21:58

标签: javascript arrays json node.js

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,其中可怜的Adam出现在敌人数组中。

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

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

如果我正在搜索“Albert”,那么它的工作完全正常,因为他只在enemies数组中出现过一次。

但是当谈到像“Adam”这样的查询时,我的函数在数据数组的第三个对象(id = 2)中找到第一个正确命中并拒绝继续,输出为{{1当在下一个对象中确实存在另一个“Adam”时,我希望结果是这样的:

2

为了实现这种类型的行为,我尝试在我正在使用的函数中插入更多['2', '3'] for循环,但是失败了。我也尝试使用一些第三方节点包来搜索我的数据数组,但也没有成功。

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

P.S。:我自己控制JSON数据的外观,所以如果问题隐藏在它的编写方式中,我可以轻松更改它,请告诉我如何。

非常感谢您的关注!

2 个答案:

答案 0 :(得分:1)

实际上它会在第一场比赛中停止,因为你将返回第一场比赛...

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

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

答案 1 :(得分:0)

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