DEV Community

Cover image for How to get the index of an object from an array of objects in JavaScript?
MELVIN GEORGE
MELVIN GEORGE

Posted on β€’ Originally published at melvingeorge.me

2 1

How to get the index of an object from an array of objects in JavaScript?

Originally posted here!

To get an index of an object from an array of objects, we can use the findIndex() array method on the array containing the object elements in JavaScript.

TL;DR

// Array of objects
const objsArr = [
  { name: "John Doe", age: 23 },
  { name: "Roy Daniel", age: 25 },
];

// Get the index of the object in the array
const indexOfObject = objsArr.findIndex((obj) => {
  // if the current object name key matches the string
  // return boolean value true
  if (obj.name === "Roy Daniel") {
    return true;
  }

  // else return boolean value false
  return false;
});

console.log(indexOfObject); // 1
Enter fullscreen mode Exit fullscreen mode

For example let's say we have an array of objects like this,

// Array of objects
const objsArr = [
  { name: "John Doe", age: 23 },
  { name: "Roy Daniel", age: 25 },
];
Enter fullscreen mode Exit fullscreen mode

Now, If we want to get the index of the object with the name key that matches Roy Daniel, we can use the findIndex() method.

  • the findIndex() requires a function as an argument.
  • the argument function will be passed the current array element every time an element is looped.
  • Inside this function, we can check if the name matches Roy Daniel and return the boolean value true if it matches and the boolean value false if not.
  • the findIndex() method returns the index of the object in the array.

It can be done like this,

// Array of objects
const objsArr = [
  { name: "John Doe", age: 23 },
  { name: "Roy Daniel", age: 25 },
];

// Get the index of the object in the array
const indexOfObject = objsArr.findIndex((obj) => {
  // if the current object name key matches the string
  // return boolean value true
  if (obj.name === "Roy Daniel") {
    return true;
  }

  // else return boolean value false
  return false;
});

console.log(indexOfObject); // 1
Enter fullscreen mode Exit fullscreen mode

That's all πŸ˜ƒ!

See the above code live in JSBin

Feel free to share if you found this useful πŸ˜ƒ.


Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly β€” using the tools and languages you already love!

Learn More

Latest comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay