indexOf method in an object array?

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

    You can create your own prototype to do this:

    something like:

    Array.prototype.indexOfObject = function (object) {
        for (var i = 0; i < this.length; i++) {
            if (JSON.stringify(this[i]) === JSON.stringify(object))
                return i;
        }
    }
    

提交回复
热议问题