Move item in array to last position

后端 未结 5 1572
终归单人心
终归单人心 2021-02-02 08:11

I have an array of objects. I want to move a selected object to the last position in the array. How do I do this in javascript or jquery?

Here is some code I have:

5条回答
  •  情歌与酒
    2021-02-02 09:01

    This is more clean, without using the array index of [0]

    const colors = ['white', 'black', 'red', 'blue', 'green'];
    
    // will push the blue to the end of the array
    colors.push(colors.splice(colors.indexOf('blue'), 1).pop());
    
    console.debug(colors);
    // ["white", "black", "red", "green", "blue"]
    

提交回复
热议问题