Sort array by order according to another array

后端 未结 6 1484
谎友^
谎友^ 2021-02-09 14:49

I have an object that is being returned from a database like this: [{id:1},{id:2},{id:3}]. I have another array which specified the order the first array should be

6条回答
  •  鱼传尺愫
    2021-02-09 15:05

    Why not just create new array and push the value from second array in?? Correct me if i wrong

    array1 = [];
    array2 = [2,3,1];
    
    for ( var i = 0; i < array2 .length; i++ )
    {
        array1.push({
             id : array2[i]
         })
    }
    

提交回复
热议问题