json index of property value

后端 未结 6 1378
春和景丽
春和景丽 2020-12-18 08:27

I need to get the index of the json object in an array whose by the objects id

here is the example code

var list = [ { _id: \'4dd822c5e8a6c42aa7000         


        
6条回答
  •  借酒劲吻你
    2020-12-18 09:25

    function index_of(haystack, needle) {
        for (var i = 0, l = haystack.length; i < l; ++i) {
            if( haystack[i]._id === needle ) {
               return i;   
            }
        }
        return -1;
    }
    
    console.log(index_of(list, '4dd80b16e8a6c428a900007d'));
    

    same as the above, but caches the length of the haystack.

提交回复
热议问题