Last active
March 3, 2021 21:23
-
-
Save TRFN/4ecefb7249b2d2635baa99eb74ef0f5f to your computer and use it in GitHub Desktop.
Array reorder function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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