lodash / lodash

A modern JavaScript utility library delivering modularity, performance, & extras.

Home Page:https://lodash.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

blindstuff opened this issue · comments

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

Thanks @blindstuff!

This looks like a nice complement to the _.insert proposal. While working with splice is a bit cumbersome I'll defer to it for now. We'll keep an eye on the popularity of the request.

I created a repo with simple tests here and published lodash-move on npm. Hope it helps.

As noted by @granteagon, this would be a nice touch for drag and drops :)

commented

Would it make more sense to just add a permutation function? Reorders arrays based on a permutation function or re-index array? Maybe a list of swaps to perform or what have you?

const arr = [1, 2, 3, 4, 5]
_.permute(arr, [0, 2, 1, 3, 4]); // [1, 3, 2, 4, 5]

const fn = (idx) => idx + 1;
_.permute(arr, fn); // [_, 1, 2, 3, 4, 5] ?

No matter what, questions are:

  • What do you expect to happen if you move out of bounds?
  • What happens if you move multiple elements to the same index?
  • Can you only do 1 swap at a time?
  • Does it make sense to just be _.move(arr, fromIdx, toIdx) or something more... useful?

@c-dante I think undefined stuff should either raise an error or just insert undefined values. I like the permute idea, but a definite flaw I see is the array move is hand coded. If you were to use it in real code, wouldn't you still need to .slice() or something to get the array of numbers illustrated above?

commented

You can implement it in a few ways -- especially since you're creating a new array each time.

Your implementation uses spread a bunch to reorder it, you could use the permute array to select elements, I'd avoid splice since it mutates the input (although that might be desired in some cases).

Yes, my example is hard coded, but there's no reason you the signature has to be that. Second example is a mapping function that goes from old to new index. Alternatively, you can have a vararg of swaps.

_.permute(arr, newIndices);
_.permute(arr, ...swaps);
_.permute(arr, mapFn);

are a few signiture ideas.

I like the _.permute idea as well as long as it remains easy for "simple swaps" (drag and drops). But it looks like _.permute(arr, ...swaps) has this covered.

Interesting. That would still work for the drag n' drop examples above so I'm good. permute is a better name than arrayMove IMO.

@c-dante et. al, I created a _.permute mixin on github and npm you can find here: https://github.com/granteagon/lodash-permute

Feedback welcome!

@c-dante slice doesn't mutate the input.

Anyone, why move function is still not a part of lodash?

Does this feature request still don't have enough likes? Whats the state?

Using https://www.npmjs.com/package/lodash-move in the meantime but would love to see this in lodash officially!

how many likes should we gather

@stepovat There is no set number. Likes are used as an indicator of popularity of a request to help make API decisions as they come.

_.permute is probably the better one to use if it does get added eventually.

@jdalton This seems quite popular and have a mixin already, and still no joy? wow

this would be useful and should be very simple to implement. also an api for _.swap(array, index1, index2) would be very useful and shouldn't be to hard to implement

@lyle45 agreed. Very useful for drag and drop. Just not sure that the bloat to the base library is necessary.

To me, the question is how many different kinds of apps need these two bits of functionality. The only time I've required it is in implementing drag/drop on a list. I'm not sure there are greater than 10-15% of apps that have something like that. So I understand why it hasn't been added yet.

It's a balancing act. I'm sure @jdalton is just guarding against excessive bloat.

What we're wanting is the benefit knowing that lodash is the #1 function library and everything there is rock solid with no worries about the maintainer/bugs.

With module loading in it adds zero bloat if you only load in what you need. Sure not everyone who uses lodash uses it (I'm sure it's a low adoption rate). It'd be great if lodash would be able to add in these functions that would even be helpful if 10-15% of it's users wanted it but would only be available for module loading.

@jdalton I feel like it is worth noting that this issue is the highest ranked feature request in the closed backlog based on reactions: https://github.com/lodash/lodash/issues?q=is%3Aissue+is%3Aclosed+sort%3Areactions-%2B1-desc

Thanks for all your work on lodash and esm. 👏

@brianespinosa he went pretty inactive around April, this needs some community love :\

Ramda provides a move function: https://ramdajs.com/docs/#move

is move function implemented?

@pycraft114 Nope. Not yet.

commented

@pycraft114 check out #89 for the open thread.

@jdalton Long time and numerous votes, isn't it time to implement this function? I can do that if you think votes are enough.

This seems like a no-brainer to add ❤️

commented

Came here today for this 😢

5 Years later from original thread, still nothing accomplished?

heuuum serious, no swap sugar coding ??? common

Nothing yet?

Would love to see it in #2020

I also, would enjoy this

Hi from 2021! Starting this discussion again after 6 years after the initial date.

Bumping this again, 🤞 we get this in 2021

As Yoda says - Always hope, there is

2021, nothing yet? Whoa, 5+ years and no work on this so far...

Still looking for this feature to be implemented. Please loadash make a move.

As drag and drop features using the help of array and objects are more on the boat, So this should be implemented

I would love to see this.

@jdalton: Landed on this issue after reading about "lodash style issue management" in the 11ty repo (here, specifically). I like the concept, but came here to see how it's working for you.

Given that this issue (top-voted FR for several years) hasn't been acted on, does that mean you've given up on this practice? Would you recommend it for other projects? Any caveats or pitfalls to be aware of?

shocked this hasn't made it yet

This is a very important feature and it will actually be used in a lot of implementations. Still waiting.... :)

Same here. Would love to see this feature.

commented

I'm waiting, too...

Well... I have exactly that kind of thing to 'build', which is of course a no brainer, but thought 'oh well, maybe lodash...'

and there is a request, many people, since 2015... come on guys, in what strange world would _.move or _.permute be bloating the library, when you have such things as _.isNull that brings 0 thing to the world 😄

It would be a very much handy function to have as a part of lodash...

Hi, please forgive if "bump+1" comments on issues is the wrong kind of vote (per your docs), but am not sure given the radio silence - so I'm adding both.

I also wanted to also thank you (the maintainers) and y'all(?) (the community) so damn much for creating and maintaining js' missing standard library, and pushing the js world further into the world of immutable fn-ish programming - I know I personally owe a lot of my love for that world (and transition into clojure prob lol) to lodash.

move will be helpfull for slide component...
live exemple here :
https://persona-dance.jp/p3d/character/

tricky animation you know : / .

The corona came and is almost gone and we are still waiting..

This looks like a nice complement to the _.insert proposal. While working with splice is a bit cumbersome I'll defer to it for now. We'll keep an eye on the popularity of the request.

@jdalton How many reactions would be considered as popular? And if you see it as a popular request now, would be there any plan to implement this?

Seriosly, such a simple and usefull util func, and no feedback after 7 years?

Still nothing? Its a really useful function

Ha, just came here because I expected Lodash to have this function. Seems more useful than a lot of other Lodash methods.

Oh wow really? I hardly understand how this can be overlooked

I opened a PR here.

I remade another PR after the issues-bankruptcy here 🤞.