JavaScript move an item of an array to the front

前端 未结 17 947
终归单人心
终归单人心 2020-12-12 20:15

I want to check if an array contains \"role\". If it does, I want to move the \"role\" to the front of the array.

var data= [\"ema         


        
17条回答
  •  渐次进展
    2020-12-12 20:52

    let data = [0, 1, 2, 3, 4, 5];
    let index = 3;
    data.unshift(data.splice(index, 1)[0]);
    // data = [3, 0, 1, 2, 4, 5]
    

提交回复
热议问题