Javascript 找到对象的索引数组angularjs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39998961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find index of the object an array angularjs
提问by Edu Arif
for example I have this array :
例如我有这个数组:
$scope.records = [
{
"Name" : "Alfreds Futterkiste",
"Country" : "Germany"
},
{
"Name" : "Berglunds snabbk?p",
"Country" : "Sweden"
},
{
"Name" : "Centro comercial Moctezuma",
"Country" : "Mexico"
},
{
"Name" : "Ernst Handel",
"Country" : "Austria"
}
];
How to get value index from object? with example "Country" : "Austria" this index is 3
如何从对象中获取值索引?例如“国家”:“奥地利”这个指数是 3
回答by adeneo
You could use Array.findIndex
in the latest browsers, but there's no support for this in Internet Explorer, only Edge
您可以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
For support in IE9 and up, you could use Array.some
instead
对于 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);
回答by Rajesh
You can use array.findIndex
for this:
您可以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)
Note: array.findIndexis a part of ES6.
注意:array.findIndex是 ES6 的一部分。
回答by Chetan
Use findIndex method -
var index = $scope.records.findIndex(x=>x.Country==='Austria')
回答by randominstanceOfLivingThing
You can do which returns the countries and then finds the country.
您可以执行 which 返回国家,然后找到国家。
$scope.records.map((item) => { return item.Country;}).indexOf('Austria')
The problem with the above code iterates once through each element to generate a map and then finds the index. You can use a simple for loop:
上面代码的问题是遍历每个元素一次生成map,然后找到索引。您可以使用一个简单的 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;
}
I noticed a lot of findIndex solutions. Please note: FindIndex method has been added to the ECMAScript 6 specification and may not be available in all JavaScript implementations yet as per MDN.
我注意到很多 findIndex 解决方案。请注意:FindIndex 方法已添加到 ECMAScript 6 规范中,根据 MDN,它可能尚未在所有 JavaScript 实现中可用。
You can use the polyfill for findIndex(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) and use the findIndex solution that others suggested. If you do not want to use the polyfill you can use the map and indexOf that I put as solution.
您可以将 polyfill 用于 findIndex( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) 并使用其他人建议的 findIndex 解决方案。如果你不想使用 polyfill,你可以使用我作为解决方案的 map 和 indexOf。
回答by Sid
Using a for - in loop as it is a JSON array, you can do this
使用 for - in 循环,因为它是一个 JSON 数组,你可以这样做
for(i in records) {
if(records[i].country=="Austria")
console.log("index is :"+ parseInt(i)); // print index
}
回答by Siddharth
I have registered this method and it works like charm,
我已经注册了这个方法,它的作用就像魅力一样,
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
回答by Makarov Sergey
function getIndexByCountryName(country)
var found = $scope.records.find(function(item){return item.Country === country});
return $scope.records.indexOf(found);
}