SoFunction
Updated on 2025-02-28

How to get index of object in array

Get the index of the object in the array

Requirements: The elements in the array are objects, and the index of the object in the array is needed.

Method: Use the findIndex method

Original data:

const  array = [
    {
        id:1,
        name:'Zhang xx'
    },
    {
        id:2,
        name:'Wang xx'
    }
]

method:

const index = (function(val){
    return  === 1 
})
// If there are multiple satisfactories, return the first index(index) // 0

indexOf() method of array

var arr = [10, 20, 30, 40, 50, 20, 56, 34, 20];
        // var ret = (); // Inversion of array, array name.reverse()        // (ret);
 
        // Find out if an element is in the array        // Array name.includes(element).  The return value of this method is Boolean false or true        /* var ret = (40); // Array name.includes(element)
         (ret); */
 
        // Get the index when an element appears in the array for the first time        // Array name.indexOf(); The return value is the subscript of the array; if it is not present, it returns -1        var ret = (20);
        (ret);
 
        var ret1 = (20, 5);   // The second parameter indicates which subscript starts from, searches, and contains the element of this subscript        (ret1);

The above is personal experience. I hope you can give you a reference and I hope you can support me more.