-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Closed
Labels
Description
I needed this in the implementation of a drag and drop gallery and found it was not on the lodash docs. If this already exists or is implemented in a different way I apologize as I could not find it.
A simple method to move an element inside of an array from one position to another.
_.move = function(array,fromIndex,toIndex){
array.splice(toIndex, 0, array.splice(fromIndex, 1)[0] );
}
Example usage
_.move([1,2,3,4,5],0,2);
// -> [2,3,1,4,5]
Solution taken from (and more detail / verifications needed available at):
http://stackoverflow.com/a/5306832/474883
benmccormick, shedar, superandrew213, dbettini, vladimyr and 634 morepajics, szczepcio95, hossein, RigoOnRails, Zhuvikin and 30 morezocoi, golestanirad, Nazzanuk, wuyiadepoju, sarendipitee and 18 moreImanMahmoudinasab, gormonn, Heilemann, devcatalin, riotrah and 3 more