Get Index of Object in Array javaScript

Finding the index of objects in a JavaScript array is a very frequently asked query. In this tutorial, we will show you two javascript functions, To get the position or index of the object in an array using JavaScript array method by key, id, name, and value property.

How to Get Index of Object in Array javaScript

Here are two JavaScript functions and multiple approaches to get the position or index of objects from an array by key, id, name, and value property:

  • FindIndex() Array in Js
  • IndexOf() Array in Js

FindIndex() Array in Js

In JavaScript, the findIndex function helps you to find the position (index) of the first element in an array that satisfies a specific condition.

Here are multiple approaches to get the index of the object in array javascript by id, key, name, value, etc using findIndex() function:

Approach 1: Find the index of the object in the js array by id using findIndex()

If your objects have an ‘id’ property, you can find the index based on the id:

function findIndexById(array, id) {
  return array.findIndex(obj => obj.id === id);
}

// Example usage:
const array = [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 3, name: 'Doe' }];
const indexById = findIndexById(array, 2);
console.log('Index by ID:', indexById);

Approach 2: Find the index of the object in the js array by key using indexOf()

Here is an example, you can find the index of an object using the key:

function findIndexByKey(array, key, value) {
  return array.findIndex(obj => obj[key] === value);
}

// Example usage:
const array = [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 3, name: 'Doe' }];
const indexByKey = findIndexByKey(array, 'id', 2);
console.log('Index by Key:', indexByKey);

Approach 3: Find the index of the object in the js array by name using findIndex()

if your objects have a ‘name’ property, you can find the index based on the name:

function findIndexByName(array, name) {
  return array.findIndex(obj => obj.name === name);
}

// Example usage:
const array = [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 3, name: 'Doe' }];
const indexByName = findIndexByName(array, 'Jane');
console.log('Index by Name:', indexByName);

indexOf() Array in Js

The indexOf() the function helps us to get the first index of the specified element from js array. If the element is not found, it returns -1.

Approach 1: Find the index of the object in the js array by name using indexOf()

If you want to find the index based on a general value, you can use the indexOf method directly:

function getIndexByValue(array, valueToFind) {
  return array.indexOf(valueToFind);
}

// Example usage
const myArray = [10, 20, 30, 40, 50];
const indexOfValue = getIndexByValue(myArray, 30);
console.log('Index by Value:', indexOfValue); // Output: 2

Conclusion

In this tutorial, you have learned how to find the position of the object from a JavaScript array by using indexOf(), findIndex() functions.

Recommended JavaScript Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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