查找数组angularjs对象的索引
find index of the object an array angularjs
例如,我有这样一个数组:
$scope.records = [
{
"Name" : "Alfreds Futterkiste",
"Country" : "Germany"
},
{
"Name" : "Berglunds snabbköp",
"Country" : "Sweden"
},
{
"Name" : "Centro comercial Moctezuma",
"Country" : "Mexico"
},
{
"Name" : "Ernst Handel",
"Country" : "Austria"
}
];
如何从对象中获取值索引?以"Country": "Austria"为例,此索引为3
您可以在最新的浏览器中使用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);
您可以使用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)
注意:数组。findinindex是ES6的一部分
Use findIndex method -
var index = $scope.records.findIndex(x=>x.Country==='Austria')
function getIndexByCountryName(country)
var found = $scope.records.find(function(item){return item.Country === country});
return $scope.records.indexOf(found);
}
你可以这样做,返回国家,然后找到国家。
$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实现中可用。
您可以使用findIndex(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex)的填充,并使用其他人建议的findIndex解决方案。如果你不想使用polyfill,你可以使用map和indexOf,我把它作为解决方案。
我已经注册了这个方法,它像魅力一样工作,
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
使用for - in循环,因为它是一个JSON数组,你可以这样做
for(i in records) {
if(records[i].country=="Austria")
console.log("index is :"+ parseInt(i)); // print index
}
相关文章:
- 在JavaScript中通过索引从对象数组中获取值
- Javascript:根据对象的嵌套数组中的值,在数组中查找对象的索引
- “未定义的索引:itemdetails”,当将JSON对象从JavaScript发送到PHP时
- 在知道对象值的情况下,确定数组中JS对象的索引
- 索引IndexedDB中对象中的数组值
- 对象中的索引错误
- 按键查找数组中对象的索引
- 使用Handlebars获取同级JSON数据,其中单个对象没有父索引键,但多个项有一个索引键
- 最新typescript编译器版本(0.9.5)中的可索引对象
- jQuery按索引不一致的值筛选对象
- 对象的角度ng重复索引
- 如何将数组的对象与同一索引组合
- 在Javascript中,有没有一种方法可以用一个语句创建一个索引对象数组
- 如何从 0 开始重新索引对象
- JavaScript - 将两个数组对象合并为一个arr对象,然后遍历每个索引/对象
- Javascript数组索引对象
- 我可以在mongo查询中索引对象吗?
- IndexedDB:索引对象值
- 查找javascript的索引"对象数组"基于对象字段值
- 在javascript中创建两个数组的字符串索引对象