Get Index of Object in Array javaScript

[ad_1]

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.

Ezoic

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:

Ezoic

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:

Ezoic

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.

Ezoic

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.

Ezoic

Recommended JavaScript Tutorials

Ezoic

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
Jassweb, Rai Chak, Punjab, India. 143518