Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptJavaScript indexOf() method in an object Array

JavaScript indexOf() method in an object Array

In this article, we will learn about the indexOf() method in an Object array in Javascript. The indexOf() methods provide the first index at which the given element exists, and -1 in case it is not present in the array.

Syntax:

indexOf(element)
indexOf(element, start);

Parameters:

  • element: the element to be searched in the input array
  • start: the index from which the search has to begin. The default value is set to 0.

Return value:

JavaScript indexOf() method returns the value of the index at which the element is present, and -1 when the element doesn’t exist.

To access the index of the object from the array having a value of an object, we are going to use the JavaScript map method to implement the indexOf() method

JavaScript map() Method:

This method creates a separate array and calls a function for every array element. This method calls the specified function once for every element in the array, maintaining the order. 

Syntax:

Array.map(function(cValue, ind, Arr), tValue)

Example 1: This example gets the index of val_32 by the map() method.

Javascript




let GFG_Array = [
    {
        prop_1: "val_11",
        prop_2: "val_12",
    },
    {
        prop_1: "val_21",
        prop_2: "val_22",
    },
    {
        prop_1: "val_31",
        prop_2: "val_32",
    },
];
console.log(JSON.stringify(GFG_Array));
 
function gfg_Run() {
    let pos = GFG_Array.map(function (e) {
        return e.prop_2;
    }).indexOf("val_32");
    console.log(
        "Index of 'val_32' value object is = " + pos
    );
}
gfg_Run();


Output

[{"prop_1":"val_11","prop_2":"val_12"},{"prop_1":"val_21","prop_2":"val_22"},{"prop_1":"val_31","prop_2":"val_32"}]
Index of 'val_32' value object is = 2

Example 2: This example doesn’t get the value so returns the -1 and prints Not Found!

Javascript




// In put array of objects
let GFG_Array = [
    {
        prop_1: "val_11",
        prop_2: "val_12",
    },
    {
        prop_1: "val_21",
        prop_2: "val_22",
    },
    {
        prop_1: "val_31",
        prop_2: "val_32",
    },
];
// Display Input
console.log(JSON.stringify(GFG_Array));
 
// Function to check index
function gfg_Run() {
    let pos = GFG_Array.map(function (e) {
        return e.prop_2;
    }).indexOf("val_42");
    if (pos == "-1") pos = "Not Found";
     
    // Display output
    console.log(
        "Index of 'val_42' value object is = " + pos
    );
}
gfg_Run();


Output

[{"prop_1":"val_11","prop_2":"val_12"},{"prop_1":"val_21","prop_2":"val_22"},{"prop_1":"val_31","prop_2":"val_32"}]
Index of 'val_42' value object is = Not Found

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!

RELATED ARTICLES

Most Popular

Recent Comments