How to get index of object by its property in JavaScript?

get index of object in array javascript
javascript find index of object in array by property
indexof javascript
get first property name of object javascript
remove element from array javascript
javascript get object property name by index
how to get index of array of object
find index of object in array angular 2

For example, I have:

var Data = [
  { id_list: 1, name: 'Nick', token: '312312' },
  { id_list: 2, name: 'John', token: '123123' },

Then, I want to sort/reverse this object by name, for example. And then I want to get something like this:

var Data = [
  { id_list: 2, name: 'John', token: '123123' },
  { id_list: 1, name: 'Nick', token: '312312' },

And now I want to know the index of the object with property name='John' to get the value of the property token.

How do I solve the problem?

As the other answers suggest, looping through the array is probably the best way. But I would put it in it's own function, and make it a little more abstract:

function findWithAttr(array, attr, value) {
    for(var i = 0; i < array.length; i += 1) {
        if(array[i][attr] === value) {
            return i;
    return -1;

var Data = [
    {id_list: 2, name: 'John', token: '123123'},
    {id_list: 1, name: 'Nick', token: '312312'}

With this, not only can you find which one contains 'John' but you can find which contains the token '312312':

findWithAttr(Data, 'name', 'John'); // returns 0
findWithAttr(Data, 'token', '312312'); // returns 1
findWithAttr(Data, 'id_list', '10'); // returns -1

EDIT: Updated function to return -1 when not found so it follows the same construct as Array.prototype.indexOf()

JavaScript, The task is to get the index of the object from the array of objects of the given property name and property value using javascript. we're going to discuss few� The task is to get the index of the object from the array of objects of the given property name and property value using javascript. we’re going to discuss few techniques. First few methods to know. JavaScript Array map() Method: This method creates a new array with the return value of calling a function for every array element.

Since the sort part is already answered. I'm just going to propose another elegant way to get the indexOf of a property in your array

Your example is:

var Data = [
    {id_list:1, name:'Nick',token:'312312'},

You can do:

var index = { return; }).indexOf('Nick'); is not available on IE7 or IE8. ES5 Compatibility

And here it is with ES6 and arrow syntax, which is even simpler:

const index = =>'Nick');

Array.prototype.findIndex(), Get the index of the first element in the array that has a value of 18 or more: value, findIndex() returns the index of that array element (and does not check the � Definition and Usage The findIndex () method returns the index of the first element in an array that pass a test (provided as a function). The findIndex () method executes the function once for each element present in the array:

If you're fine with using ES6. Arrays now have the findIndex function. Which means you can do something like this:

const index = Data.findIndex(item => === 'John');

JavaScript Array findIndex() Method, JavaScript has a rudimentary object iteration mechanism built into it so if you have an object and you want to get at all the properties and values� JavaScript provides a bunch of good ways to access object properties. The dot property accessor syntax works nicely when you know the variable ahead of time. When the property name is dynamic or is not a valid identifier, a better alternative is square brackets property accessor: object[propertyName].

var index = Data.findIndex(item => == "John")

Which is a simplified version of:

var index = Data.findIndex(function(item){ return == "John"})


The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.

re: Getting JavaScript Properties for Object Maps by Index or Name, How can I get the index of an obj in an array of objects? const arr = [ {x: "a", y: 1}, { x: "b", y: 2} ] const i = arr.indexOf({x: "a", y: 1}) console.log(i)� Getting value out of maps that are treated like collections is always something I have to remind myself how to do properly. In this post I look at JavaScript object iteration and picking out values from a JavaScript object by property name or index.

If you're having issues with IE, you could use the map() function which is supported from 9.0 onward:

var index = =>"Nick");

Get index of object in array - JavaScript, This method returns the index of the first occurrence the element that you want you have the following array of objects, where each object has two properties:� The Object.values() method returns an array of a given object's own enumerable property values. So if you have the same object but use values instead, const object1 = { a: 'somestring', b: 42, c: false }; console.log(Object.values(object1)); You would get the following array: [ 'somestring', 42, false ]

JavaScript Array indexOf and lastIndexOf: Locating an Element in an , The indexOf method will return the index of the first element which matches your search. Or if it does not find a match it will return -1 . It also gives you the ability to � EDIT 2: @bitwiseplatypus brings up the situation that would occur should someone add properties/methods to your objects at a point in time later than when you originally wrote your objects (via its prototype) - while it is true that this might cause unexpected behavior, I personally don't see that as my problem entirely. Just a matter of opinion.

Finding Array Elements with Array#indexOf, The DOM element or first element within the jQuery object to look for. Return Values. If no argument is passed to the .index() method, the return value is an integer indicating the <script src=""></ script> Properties of jQuery Object Instances � Properties of the Global jQuery Object. At the implementation level, JavaScript's arrays actually store their elements as standard object properties, using the array index as the property name. The length property is special. It always returns the index of the last element plus one. (In the example below, 'Dusty' is indexed at 30, so cats.length returns 30 + 1).

1.0.index( element ), Get code examples like "get index of object in array javascript" instantly right from your google search results with the Grepper Chrome Extension. For accessor properties, there is no value or writable, but instead there are get and set functions. That is, an accessor descriptor may have: get – a function without arguments, that works when a property is read, set – a function with one argument, that is called when the property is set, enumerable – same as for data properties,

  • Why do you want to sort the list first before searching for the property?
  • JavaScript objects are {Key:Value}, I fixed it for you.
  • possible duplicate of Search JSON array for matching attribute
  • If you scan through the answers, it appears like there is some native Data object. It is merely a capitalized variable name, which is against convention. If anyone else is bothered by this, I will make edits to the question and answers to fix this naming.
  • Related question:…
  • Added answer extending this to cover searching deeper than one attribute level. Thanks for this Chris - really helped :D
  • To those who will be having problem with this solution, remember that the === equal sign will not only check the value but also the datatype. so comparing date, numbers and empty values might return false if the actual value is a string. You can use == equal sign if the datatype is of no essence for you.
  • DON'T DO THIS. Learn how to use higher-order functions this is the old way of doing things.
  • I have to iterate the entire array no matter what. The first method don't need to.
  • Simple, but take into account the performance when using large datasets
  • Really great answer helped me with this problem!
  • This solution is lovely. May you live to be a thousand years old, sir.
  • Doing a map and then indexOf in the ES6 version does mean they'll be looping over the array twice. Instead you should use the findIndex method shown in my answer