[removed] How can I swap elements of an array of objects (by reference, not index)?

前端 未结 4 1128
挽巷
挽巷 2021-01-22 11:55

I have an array of objects a=[ {v:0}, {v:1}, {v:2}, {v:3} ] ;

I do not have the index into the array, but I do have references to

4条回答
  •  迷失自我
    2021-01-22 12:04

    Here is a one liner, just in case:

    a.splice(a.indexOf(s2), 1, a.splice(a.indexOf(s1),1,s2)[0]);
    

提交回复
热议问题