Someone in another component has already shown how to implement times and debounce.Get can be achived with optional chaining, so heres how to do keyBy:
times
debounce
keyBy
const keyBy = (arr, key) => Obhect.fromEntries(arr.map(o => [o[key], o]))
...and with types:
const keyBy = <T extends object, K extends keyof T>(arr: T[], key: K): Record<T[K] extends (string | number) ? T[K] : never, T>
(Have't tested this since I'm on mobile rigut now, but you yet the idea)
There’s no way to do this easily with plain JavaScript without writing multiple lines of code
Since its doable in 1 line your statemet kinda false
To avoid Object.fromEntries, there is .reduce.
Object.fromEntries
.reduce
arr.map(o => [o[key], o]).reduce((prev, [k, v]) => { ...prev, [k]: v }, {})
Actually, with map, filter, reduce -- you can do a lot.
It is impossible to say that you cannot do something in one-liner JavaScript, because minified JS is also a one liner.
I agree. I think there're things like differenceWith in Lodash that are harder to implement by ourselves and not available in the standard library.
differenceWith
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.
Someone in another component has already shown how to implement
times
anddebounce
.Get can be achived with optional chaining, so heres how to dokeyBy
:...and with types:
(Have't tested this since I'm on mobile rigut now, but you yet the idea)
Conclusion
Since its doable in 1 line your statemet kinda false
To avoid
Object.fromEntries
, there is.reduce
.Actually, with map, filter, reduce -- you can do a lot.
It is impossible to say that you cannot do something in one-liner JavaScript, because minified JS is also a one liner.
I agree. I think there're things like
differenceWith
in Lodash that are harder to implement by ourselves and not available in the standard library.