indexOf method in an object array?

前端 未结 27 2427
别跟我提以往
别跟我提以往 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:31

    This is the way to find the object's index in array

        var myArray = [{  hello: 'world',
            foo: 'bar'
        },{
            hello: 'stevie',
            foo: 'baz'
        }];
    
    
    
        for (i = 0; i < myArray.length; i++) {
            if (myArray[i].hello === 'stevie') {
                alert('position: ' + i);
                return;
            }
        }
    

提交回复
热议问题