json index of property value

后端 未结 6 1377
春和景丽
春和景丽 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:00

    Just loop through the list until you find the matching id.

    function indexOf(list, id) {
      for (var i = 0; i < list.length; i++) {
        if (list[i]._id === id) { return i; }
      }
      return -1;
    }
    

提交回复
热议问题