indexOf method in an object array?

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

    This works without custom code

    var arr, a, found;
    arr = [{x: 1, y: 2}];
    a = {x: 1, y: 2};
    found = JSON.stringify(arr).indexOf(JSON.stringify(a)) > - 1;
    // found === true
    

    Note: this does not give the actual index, it only tells if your object exists in the current data structure

提交回复
热议问题