I was helping a user over at sanity.io yesterday doing some content migration, where they wanted to take the last element of an array and put it in...
For further actions, you may consider blocking this person and/or reporting abuse
Why not use a really simple solution with plain ES5?
I mean it would be ES5 if
const
wasvar
. 😂Anyway, I like how concise it is. Three lines of code sure does look more satisfying than whatever I did, despite their similarities.
I agree this is readable.
If you handed this to a new developer and asked them what it did they might be lost for a little while.
Also, that would change the original array, which is not the intended functionality.
Good point, you'd have to do something like
which is even more convoluted.
How about
const lastToFirst = _ => ($ => [$.pop(), ...$])(_.slice())
as even more convoluted example?Yikes...
I kinda love it.
This was literally my first thought when I was reading the problem.
Yeah that is it.
This article is lol though. I feel like the article author is trolling.
Especially when I read: "Every time I get a question like this, I like to see if I manage to solve it without going to any libraries".
It wasn't trolling! 😁
It's more like “I got serious about JavaScript when the first features of ES6 and every problem seem like a nail”. And that's exactly why I asked for other approaches and learned something.
…and some meta-point about how feeling super JS savvy because I know
.reduce
, and being humbled because I don't know some super obvious ES5 features that have been around for a long while.But thanks for the input!
haha ok, but anyways I did not want to offend or anything. You took the scenic route :D
And that delivers value for future endeavours. Happy holidays to you and your family.
I think this will do just fine.
I would go for
or
ES6 FTW!
Probably that array is not going to be accessed using the index, it looks like an unordered collection.
If this is true you need a linked list not an array, making the move operation 2N more efficient and resulting in a simpler code.
Great point! Array Data Structures aren't very efficient at arbitrarily inserting or plucking (even though JS provides convenient shift/deshift methods.)
With this small of a dataset it doesn't matter, but it's a great point. :)
I very much like the plain ES5 solution by @lexlohr . Here's another, more generic version that lets you rotate an array by a variable number of positions:
Everyone is talking about your code, and I'm stuck appreciating this beautiful gem:
Thank you for noticing!