Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
blindstuff opened this issue Dec 11, 2015 · 63 comments
Closed

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

blindstuff opened this issue Dec 11, 2015 · 63 comments

Comments

@blindstuff
Copy link

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

@jdalton
Copy link
Member

jdalton commented Dec 11, 2015

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.

@granteagon
Copy link

granteagon commented Jun 13, 2016

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

@jtheoof
Copy link

jtheoof commented Jun 28, 2016

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

@c-dante
Copy link

c-dante commented Jun 29, 2016

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?

@granteagon
Copy link

@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?

@c-dante
Copy link

c-dante commented Jun 29, 2016

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.

@jtheoof
Copy link

jtheoof commented Jun 29, 2016

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.

@granteagon
Copy link

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

@granteagon
Copy link

@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!

@slowdub
Copy link

slowdub commented Mar 29, 2017

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

@eMarek
Copy link

eMarek commented Jun 15, 2017

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

@BrunnerLivio
Copy link

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

@charlieclark
Copy link

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

@stepovat
Copy link

how many likes should we gather

@jdalton
Copy link
Member

jdalton commented Sep 14, 2017

@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.

@lodash lodash deleted a comment from SwenVogel Nov 3, 2017
@granteagon
Copy link

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

@matheo
Copy link

matheo commented Dec 1, 2017

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

@lyle45
Copy link

lyle45 commented Dec 3, 2017

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

@granteagon
Copy link

@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.

@lodash lodash deleted a comment from korywka Feb 20, 2018
@CalebKester
Copy link

CalebKester commented Feb 20, 2018

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.

@lodash lodash deleted a comment from granteagon Feb 21, 2018
@villeneuve-michael
Copy link

Would love to see it in #2020

@nzayatz14
Copy link

I also, would enjoy this

@MitchellBouwman
Copy link

MitchellBouwman commented Jan 13, 2021

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

@hiporox
Copy link

hiporox commented Mar 6, 2021

Bumping this again, 🤞 we get this in 2021

@mustkem
Copy link

mustkem commented Apr 19, 2021

As Yoda says - Always hope, there is

@darkguy2008
Copy link

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

@MR-AMDEV
Copy link

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

@MR-AMDEV
Copy link

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

@gzapata2
Copy link

I would love to see this.

@broofa
Copy link

broofa commented Nov 17, 2021

@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?

@lodash lodash deleted a comment from spierala Dec 13, 2021
@koolamusic
Copy link

shocked this hasn't made it yet

@LuciKritZ
Copy link

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

@Vincz
Copy link

Vincz commented Jan 10, 2022

Same here. Would love to see this feature.

@ZaneL1u
Copy link

ZaneL1u commented Feb 17, 2022

I'm waiting, too...

@gvolluz
Copy link

gvolluz commented Mar 2, 2022

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 😄

@AndrewKiri
Copy link

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

@riotrah
Copy link

riotrah commented Mar 24, 2022

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.

@c0ncentus
Copy link

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

tricky animation you know : / .

@sarab5000
Copy link

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

@ImanMahmoudinasab
Copy link

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?

@chadwtaylor
Copy link

+1

@MSerj
Copy link

MSerj commented Jan 19, 2023

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

@CarlosVergikosk
Copy link

CarlosVergikosk commented Feb 16, 2023

Still nothing? Its a really useful function

@matthew-dean
Copy link

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

@Akronae
Copy link

Akronae commented Apr 14, 2023

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

@buondevid
Copy link

I opened a PR here.

@jegors-at-levelpath

This comment was marked as off-topic.

@buondevid
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests