indexOf method in an object array?

前端 未结 27 3212
别跟我提以往
别跟我提以往 2020-11-22 02:18

What\'s the best method to get the index of an array which contains objects?

Imagine this scenario:

var hello = {
    hello: \'world\',
    foo: \'ba         


        
27条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-22 02:54

    var idx = myArray.reduce( function( cur, val, index ){
    
        if( val.hello === "stevie" && cur === -1 ) {
            return index;
        }
        return cur;
    
    }, -1 );
    

提交回复
热议问题