Direct answer: Use map to transform every element, filter to keep some, reduce to collapse many into one, and find / some / every to ask questions without…
Since ES2023, toSorted, toReversed, toSpliced, and with return copies have been available, so you can drop the [...arr].sort() workaround. Run ['1','2','3'].map(parseInt) in any console, and you get [1, NaN, NaN]. The map passes (value, index, array) to the callback, and parseInt reads the index as its radix argument. It is a static method on Object, not an array method, and it returns a null-prototype object.
Key takeaways
- A map callback with braces and no return yields an array of undefined.
- A filter callback that returns an object is always truthy.
- Always give a reduced initial value.
Prefer toSorted / toReversed over in-place versions on shared data.
Read the full article: https://www.codetalenthub.io/js-array-methods-cheat-sheet/
Top comments (0)