I think the biggest disadvantage of sort() is that it sorts in place. You need to do some relatively ugly hacks like spreading:
sort()
[...array].sort().map(console.log)
in order to use the power of FP.
Yeah, it is a shame that it acts in place as we move to code that attempts to reduce the number of side effects. At least we can work around it, the problem is just remembering to.
it returns an array of undefineds :/
undefined
That's because the console.log returns undefined, and it's being used in the call to .map().
console.log
.map()
var array = [9, 2, 3, 8, 1, 0] var orderedArray = [...array].sort() console.log(array) // [9, 2, 3, 8, 1, 0] console.log(orderedArray) // [0, 1, 2, 3, 8, 9] var logReturn = console.log("hello world") // hello world console.log(logReturn) // undefined
exactly, my point is we should not use map here at all.
map
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I think the biggest disadvantage of
sort()
is that it sorts in place.You need to do some relatively ugly hacks like spreading:
in order to use the power of FP.
Yeah, it is a shame that it acts in place as we move to code that attempts to reduce the number of side effects. At least we can work around it, the problem is just remembering to.
it returns an array of
undefined
s :/That's because the
console.log
returnsundefined
, and it's being used in the call to.map()
.exactly, my point is we should not use
map
here at all.