The Wayback Machine - https://web.archive.org/web/20240118172430/https://www.geeksforgeeks.org/javascript-program-to-find-index-of-an-object-by-key-and-value-in-an-array/
Open In App
Related Articles

JavaScript Program to Find Index of an Object by Key and Value in an Array

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

We have given an array with multiple objects in the form of keys and values. Our task is to find the index of an object by key and value in a JavaScript language. Below is an example for better understanding.

Example:

arr = [
{ course: "DevOps", price: 11999 },
{ course: "GATE", price: 6999 },
{ course: "ML & DS", price: 5999 },
{ course: "DSA", price: 3999 },
];
Input:
key = "course";
value = "DSA";
Output: 3
Explanation: name : DSA object is at 3rd index in the array.

So let’s see each of the approaches with its implementation.

Approach 1: Using JavaScript for Loop and If Condition

In this approach, we are using the for loop and if condition to get the index of the object. Here, firstly we take the input of key and value and then we iterate over the array, by using the if condition we are checking if the key and value are been matched in the array. For example. {course: DSA} is matched, then we return its index and print the output, else if no match is found then -1 is printed.

Syntax

for (statement 1; statement 2; statement 3) {
code here...
}

Example: In this example, we will find an index of an object by key and value in a JavaScript array using Using for Loop and if Condition.

Javascript




let objArray = [
    { course: "DevOps", price: 11999 },
    { course: "GATE", price: 6999 },
    { course: "ML & DS", price: 5999 },
    { course: "DSA", price: 3999 },
];
let k = "course";
let val = "DSA";
let objIndex = -1;
for (let i = 0; i < objArray.length; i++) {
    if (objArray[i][k] === val) {
        objIndex = i;
        break;
    }
}
console.log(objIndex);


Output

3

Approach 2: Using findIndex() Method

In this approach, we use the findIndex() method. As this method is searching for the object in the array and it returns the index of the first matched object. Here, we are giving the key and the value and checking for the match.

Syntax

array.findIndex(callback(element[, index[, array]])[, thisArg])

Example: In this example, we will find an index of an object by key and value in a JavaScript array using the findIndex() Method.

Javascript




let objArray = [
    { course: "DevOps", price: 11999 },
    { course: "GATE", price: 6999 },
    { course: "ML & DS", price: 5999 },
    { course: "DSA", price: 3999 },
];
let k = "course";
let val = "DSA";
let objIndex = objArray.findIndex(
    (temp) => temp[k] === val
);
console.log(objIndex);


Output

3

Approach 3: Using Array map() and indexOf Methods

In this approach, we are using the map() method to go through the input elements and we are giving the match condtion of key and value. There is indexOf() method, which returns the index of the first matched elements. We are storing this index in the objIndex variable and printing it using log function.

Syntax

map((element, index, array) => { /* … */ })

Example: In this example, we will find the index of an object by key and value in a JavaScript array using Using map() and indexOf Methods

Javascript




let objArray = [
    { course: "DevOps", price: 11999 },
    { course: "GATE", price: 6999 },
    { course: "ML & DS", price: 5999 },
    { course: "DSA", price: 3999 },
];
let k = "course";
let val = "GATE";
let objIndex = objArray.map((temp) => temp[k]).indexOf(val);
console.log(objIndex);


Output

1

Approach 4: Using Array some() Method

In this method, we use the some() method that is used to check whether one of the object from the array satisfies the condtion specified in the argument method. Here, we are checking the condtion of key==value, and then we are returning the index of that value and storing in the varibale and printing using console.log() method.

Syntax

array.some(callback(element,index,array),thisArg)

Example: In this example, we will find an index of an object by key and value in a JavaScript array Using some() Method

Javascript




let objArray = [
    { course: "DevOps", price: 11999 },
    { course: "GATE", price: 6999 },
    { course: "ML & DS", price: 5999 },
    { course: "DSA", price: 3999 },
];
let k = "course";
let val = "DevOps";
let objIndex;
objArray.some((key, value) => {
    if (key.course == val) {
        objIndex = value;
        return true;
    }
});
console.log(objIndex);


Output

0

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 11 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials