Getting index of an array's element based on its properties

后端 未结 7 1187
名媛妹妹
名媛妹妹 2020-11-27 17:17

I have a JavaScript array of objects like this:

var myArray = [{...}, {...}, {...}];

Each object has unique id among other pro

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 17:29

    If each id is unique, you can do it like this:

    o1 = {id:1}
    o2 = {id:2}
    o3 = {id:3}
    o4 = {id:4}
    a = [o1,o2,o3,o4]
    a.indexOf( a.filter( function(i){return i.id==4} )[0] );
    

提交回复
热议问题