Find Index of the Object an Array Angularjs

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

1

7 Answers

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);
6

You can use array.findIndex for this:

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.findIndex is a part of 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);
}

You can do which returns the countries and then finds the country.

$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:

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.

You can use the polyfill for 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.

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
1

Using a for - in loop as it is a JSON array, you can do this

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest