Skip to content

Instantly share code, notes, and snippets.

@TRFN
Last active March 3, 2021 21:23
Show Gist options
  • Save TRFN/4ecefb7249b2d2635baa99eb74ef0f5f to your computer and use it in GitHub Desktop.
Save TRFN/4ecefb7249b2d2635baa99eb74ef0f5f to your computer and use it in GitHub Desktop.
Array reorder function
Array.prototype.moveElement = function(a, b, g = false){
if(typeof a == "undefined" || typeof b == "undefined"){return false}
let c = this, d = c.splice(a, 1), e = (g?c.splice(b+a, c.length-b-a):c.splice(b, c.length-b));
c.push(d[0]);
for(let f = 0; f < e.length; f++){
c.push(e[f]);
}
return c;
}
/*
USAGE: ([-Array- Object]).moveElement(position, new_position, ?relative);
-> {position}: Index of element to move
-> {new_position}: New position of element to move
-> {relative[?opt]}: If true, the new position will be equivalent to the initial position. (Default: false)
AUTHOR: Tulio Rodrigues de Freitas Nascimento
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment