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
Use findIndex method -
var index = $scope.records.findIndex(x=>x.Country==='Austria')
You could use Array.findIndex
in the latest browsers, but there's no support for this in Internet Explorer, only 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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With