Move an array element from one array position to another

后端 未结 30 3199
渐次进展
渐次进展 2020-11-22 08:36

I\'m having a hard time figuring out how to move an array element. For example, given the following:

var arr = [ \'a\', \'b\', \'c\', \'d\', \'e\'];
<         


        
30条回答
  •  生来不讨喜
    2020-11-22 09:01

    const move = (from, to, ...a) =>from === to ? a : (a.splice(to, 0, ...a.splice(from, 1)), a);
    const moved = move(0, 2, ...['a', 'b', 'c']);
    console.log(moved)

提交回复
热议问题