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\'];
<
Here is my one liner ES6 solution with an optional parameter on
.
if (typeof Array.prototype.move === "undefined") {
Array.prototype.move = function(from, to, on = 1) {
this.splice(to, 0, ...this.splice(from, on))
}
}
Adaptation of the first solution proposed by digiguru
The parameter on
is the number of element starting from from
you want to move.