查找对象的索引数组angularjs

时间:2016-10-12 12:45:58

标签: javascript angularjs arrays

例如我有这个数组:

  $scope.records = [
    {
      "Name" : "Alfreds Futterkiste",
      "Country" : "Germany"
    },
    {
      "Name" : "Berglunds snabbköp",
      "Country" : "Sweden"
    },
    {
      "Name" : "Centro comercial Moctezuma",
      "Country" : "Mexico"
    },
    {
      "Name" : "Ernst Handel",
      "Country" : "Austria"
    }
  ];

如何从对象获取价值指数?例如“国家”:“奥地利”这个指数是3

7 个答案:

答案 0 :(得分:13)

您可以使用array.findIndex



var d = [{
  "Name": "Alfreds Futterkiste",
  "Country": "Germany"
}, {
  "Name": "Berglunds snabbköp",
  "Country": "Sweden"
}, {
  "Name": "Centro comercial Moctezuma",
  "Country": "Mexico"
}, {
  "Name": "Ernst Handel",
  "Country": "Austria"
}];
var searchCountry = "Austria"
var index = d.findIndex(x=>x.Country === searchCountry)
console.log(index)




注意:array.findIndex是ES6的一部分。

答案 1 :(得分:9)

您可以在最新的浏览器中使用Array.findIndex,但在Internet Explorer中不支持此功能,只有Edge

let $scope = {};

$scope.records = [
    {
      "Name" : "Alfreds Futterkiste",
      "Country" : "Germany"
    },
    {
      "Name" : "Berglunds snabbköp",
      "Country" : "Sweden"
    },
    {
      "Name" : "Centro comercial Moctezuma",
      "Country" : "Mexico"
    },
    {
      "Name" : "Ernst Handel",
      "Country" : "Austria"
    }
];

let index = $scope.records.findIndex( record => record.Country === "Austria" );

console.log(index); // 3

要获得IE9及更高版本的支持,您可以使用Array.some代替

var $scope = {};

$scope.records = [{
  "Name": "Alfreds Futterkiste",
  "Country": "Germany"
}, {
  "Name": "Berglunds snabbköp",
  "Country": "Sweden"
}, {
  "Name": "Centro comercial Moctezuma",
  "Country": "Mexico"
}, {
  "Name": "Ernst Handel",
  "Country": "Austria"
}];

var index = -1;

$scope.records.some(function(obj, i) {
  return obj.Country === "Austria" ? index = i : false;
});

console.log(index);

答案 2 :(得分:2)

Use findIndex method - 
   var index = $scope.records.findIndex(x=>x.Country==='Austria')

答案 3 :(得分:0)

function getIndexByCountryName(country)
    var found = $scope.records.find(function(item){return item.Country === country});
    return $scope.records.indexOf(found);
}

答案 4 :(得分:0)

您可以执行哪些操作返回国家/地区,然后查找国家/地区。

$scope.records.map((item) => { return item.Country;}).indexOf('Austria')

上述代码的问题在每个元素中迭代一次以生成映射,然后查找索引。您可以使用简单的for循环:

var index = (arr, k, v) => { 
   var i = -1;len = (arr || []).length;  
   for (i = 0; i < len; i++) { 
     if (arr[i][k] === v) 
       return i;
   }
   return -1;
} 

我注意到很多findIndex解决方案。请注意:FindIndex方法已添加到ECMAScript 6规范中,并且可能并非在MDN的所有JavaScript实现中都可用。

您可以将polyfill用于findIndex(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex)并使用其他人建议的findIndex解决方案。如果您不想使用polyfill,可以使用我作为解决方案提供的map和indexOf。

答案 5 :(得分:0)

使用for - in循环,因为它是一个JSON数组,你可以这样做

for(i in records) {
if(records[i].country=="Austria") 
console.log("index is :"+ parseInt(i)); // print index
}

答案 6 :(得分:0)

我已经注册了这个方法,它就像魅力一样,

Array.prototype.getIndexByValue = function (name, value) {
        for (var i = 0, len=this.length; i <len; i++) {
            if (this[i][name]) {
                if (this[i][name] === value) {
                    return i
                }
            }
        }
        return -1;
    };

var d = [{
  "Name": "Alfreds Futterkiste",
  "Country": "Germany"
}, {
  "Name": "Berglunds snabbköp",
  "Country": "Sweden"
}, {
  "Name": "Centro comercial Moctezuma",
  "Country": "Mexico"
}, {
  "Name": "Ernst Handel",
  "Country": "Austria"
}];


var index = d.getIndexByValue('Country','Austria');
console.log(index)//will print 3