indexOf method in an object array?

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

    Or prototype it :

    Array.prototype.indexOfObject = function arrayObjectIndexOf(property, value) {
        for (var i = 0, len = this.length; i < len; i++) {
            if (this[i][property] === value) return i;
        }
        return -1;
    }
    
    myArr.indexOfObject("name", "stevie");
    

提交回复
热议问题