Skip to content

Feature Request: _.move() element in an array #1701

@blindstuff

Description

@blindstuff

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions