Sort an array of objects based on another array of ids

后端 未结 10 2119
故里飘歌
故里飘歌 2020-11-28 16:27

I have 2 arrays

a = [2,3,1,4]
b = [{id: 1}, {id: 2}, {id: 3}, {id: 4}]

How do I get b sorted based on a? My desir

10条回答
  •  借酒劲吻你
    2020-11-28 17:09

    You could just create c based off of a without ever using b:

    var a = [2,3,1,4];
    var c = [];
    
    for(var i = 0; i < a.length; i++){
    
        c.append({id:a[i]);
    
    }
    

    Hope this helps!

提交回复
热议问题