【问题标题】:How can I return an Index location for an item in a JSON array by searching with text?如何通过文本搜索返回 JSON 数组中项目的索引位置?
【发布时间】:2016-09-13 12:36:01
【问题描述】:

这是我的 JSON 数组:

var planets = [{
  "Name": "Mercury",
  "Temperature": "427°C",
  "Position" : 1
}, {
  "Name": "Venus",
  "Temperature": "462°C",
  "Position" : 2

}, {
  "Name": "Earth",
  "Temperature": "16°C",
  "Position" : 3
}]

使用文本“地球”是否有一种方法可以返回我的行星数组中地球项目的索引位置?

例如:

planets.find("Earth")

【问题讨论】:

标签: javascript arrays json


【解决方案1】:

普通 JS:findIndex

findIndex() 方法返回数组中的索引,如果在 该数组满足提供的测试功能。否则 -1 是 返回。

[{
  "Name": "Mercury",
  "Temperature": "427°C",
  "Position" : 1
}, {
  "Name": "Venus",
  "Temperature": "462°C",
  "Position" : 2

}, {
  "Name": "Earth",
  "Temperature": "16°C",
  "Position" : 3
}].findIndex(x => x.Name === "Earth")

如果您使用的是 IE 9+,则可以使用 reduce function

reduce() 方法对累加器应用一个函数,每个 数组的值(从左到右)将其减少为单个 价值。

[{
  "Name": "Mercury",
  "Temperature": "427°C",
  "Position" : 1
}, {
  "Name": "Venus",
  "Temperature": "462°C",
  "Position" : 2

}, {
  "Name": "Earth",
  "Temperature": "16°C",
  "Position" : 3
}].reduce(function (foundSoFar, x, i) { // Note no arrow funcion
  if (foundSoFar < 0 && x.Name === "Earth") {
    return i;
  } else {
    return foundSoFar;
  }
}, -1);

或者,使用像 ramda 这样的库的实现

【讨论】:

  • 什么是 findIndex?它来自哪里?你能解释一下吗?
  • 这是一个很好的答案,但链接的文章表明您不会得到 IE 的任何支持。
  • @AdamWaselnuk 现在怎么样?
  • reduce 的伟大使用 :)
【解决方案2】:

试试这个:

var index = -1;
var val = 'Earth';
var filteredObj = planets.find(function(item, i){
  if(item.Name === val){
    index = i;
    return i;
  }
});

console.log(index, filteredObj);

【讨论】:

  • 我会假设数据在我的情况下是“行星”?
【解决方案3】:

使用常规 js:

function getIndexOfPlanet(name){
  for( var i in planets )
    if( planets[i].Name == name )
       return i;

}

使用 Lodash 实用方法findIndex,就这么简单:

var planets = [{
  "Name"        : "Mercury",
  "Temperature" : "427°C",
  "Position"    : 1
}, {
  "Name"        : "Venus",
  "Temperature" : "462°C",
  "Position"    : 2
}, {
  "Name"        : "Earth",
  "Temperature" : "16°C",
  "Position"    : 3
}]

function getIndexOfPlanet(name){
  return _.findIndex(planets, { 'Name':name});
}

console.log( getIndexOfPlanet('Earth') );
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"&gt;&lt;/script&gt;

【讨论】:

    【解决方案4】:

    var locationIndex; planets.forEach(function(ele,ind){if(ele.Name === "地球") locationIndex = ind;}); console.log(locationIndex);

    find 或 findIndex 可能不支持某些浏览器,例如 IE..

    【讨论】:

      【解决方案5】:

      可以使用数组中当前正在处理的元素的Array#findindex

      var planets = [{"Name": "Mercury","Temperature": "427°C","Position": 1}, {"Name": "Venus","Temperature": "462°C","Position": 2}, {"Name": "Earth","Temperature": "16°C","Position": 3}],
          earthIndex = -1,
          earth = planets.find(function(item, index) {
              if (item.Name === 'Earth') {
                  earthIndex = index;
                  return true;
              }
              return false;
          });
      
      console.log('Earth index is:', earthIndex);

      【讨论】:

      • 这将比findIndex 得到更广泛的支持。我想我要做的唯一更改是在找不到元素的情况下返回 -1,因为这会使它的行为更像 findIndex 函数,但这可能与不同的用例相关,也可能不相关。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 2023-04-10
      • 2019-12-21
      • 1970-01-01
      相关资源
      最近更新 更多