How to get index (offset) of the array of objects when value of a property of its element object is known?

Here is the solution

First create a new array of the elements of the know property only, using the array.map() function. And then chain it with indexOf(knownValue). This will actually give u the index of the value in current new array but is the required index of the element object you are looking for.

var array = [{ name: 'abc', 
  property: 'something',
  anotherproperty: 'somethingelse'
},{},{}];

var index = array.map(function(element) {
  return element.name;
}).indexOf('abc');

 

Comments

comments

Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>