In this article, we will explore the method for retrieving the index of an object within an array in JavaScript.
There are various methods that we can use to get the index of the object in the array which are as follows:
- map method
- findIndex method
- some method
- for loop
Let us understand each method one by one.
Utilizing the map method
The map function is employed to create a new array by executing a provided function on every element of the input array. It triggers the designated function just once for each element in the array.
Syntax:
array.map(function(current_value, index, arr), thisValue)
In the syntax provided above, the subsequent parameters are elaborated upon below:
(function(current_value, index, arr): This line establishes the function that will be executed for each element in the array.
current_value: This represents the present value of the element.
index: It specifies the position of the current element within the array.
arr: It specifies an object within an array to which the present element is associated.
thisValue: This parameter specifies the value that will be supplied to the function, which will then be used as its "this" context.
The parameters "(function(currentvalue, index, arr)" and "currentvalue" are mandatory. On the other hand, the parameters "index" and "arr" are not required. When the parameter "thisValue" is not assigned any value, it defaults to undefined.
Example:
In the upcoming demonstration, we will retrieve the index of elements within an object array by employing the map method.
Code:
let arrayObjectStudent = [{
id: 100,
subject: 'Mathematics',
marks: '84'
}, {
id: 101,
subject: 'English',
marks: '72'
}];
function student() {
let property = 'subject';
let val = 'Mathematics';
console.log("Index of property = "
+ property + " with" + " " + val +
" is " +
arrayObjectStudent.map(function (e) {
return e.subject;
}).indexOf(val));
}
student();
Output:
It is evident that we have obtained the index value of the array object.
Utilizing the findIndex method
The findIndex function is employed to execute a specified function for every element within an array. It yields the initial index of the array element that satisfies the condition. In cases where the desired data is absent, it will return the value "-1".
Example:
In the upcoming demonstration, we will retrieve the index of an object within an array by employing the findIndex method.
Code:
let arrayObjectEmployee = [{
id: 501,
department: 'Software Development',
salary: 55000
}, {
id: 502,
department: 'Finance',
salary: 42000
}];
const index = arrayObjectEmployee.findIndex(object => {
return object.department === 'Software Development';
});
console.log(index);
Output:
Below is the output where we can observe that the index of the array object has been located.
Utilizing the some method
The some method is employed to determine whether at least one element within the array meets the specified condition, subsequently returning the index value of that element. It applies a function to each individual element present in the array.
Example:
In the upcoming demonstration, we will utilize the some method to retrieve the index of the object array.
Code:
let arrayObjectCars = [{
company: 'BMW',
model: 'BMW X1',
price: 'Rs. 49.50 Lakh'
}, {
company: 'Audi',
model: 'Audi S5 Sportback',
price: 'Rs. 75.80 Lakh'
}];
let index;
arrayObjectCars.some((object, idx) => {
if (object.model === 'Audi S5 Sportback') {
index = idx;
return true;
}
});
console.log(index);
Output:
In the following output, we can observe that the index of the array object has been identified.
Utilizing the for loop
The "for" loop is employed to traverse through the array elements and determine if the specified value exists within the array.
Example:
In the upcoming demonstration, we will utilize a "for" loop to retrieve the index of an array of objects.
Code:
let arrayObjBooks = [{
genre: 'Science Fiction',
name: 'Dune',
author: 'Frank Herbert'
}, {
genre: 'Mystery',
name: 'Harry Potter and the Half-Blood Prince',
author: 'J.K. Rowling'
}];
function fun(array, attr, value) {
for (let k = 0; k < array.length; k += 1) {
if (array[k][attr] === value) {
return k;
}
}
return -1;
}
function books() {
let property = 'name';
let val = 'Harry Potter and the Half-Blood Prince';
console.log("Index of property = '" +
property + "' val = '" + val + "' is = "
+ fun(arrayObjBooks, property, val));
}
books();
Output:
Below is the output that allows us to examine the indices of the array object.
Conclusion:
In this article, we have explored how to retrieve the index of an object within an array in JavaScript. We have examined different techniques, supported by examples, including the map method, findIndex method, some method, and the for loop.