Skip to content
Advertisement

Javascript .reduce() tips? Is there a way to rename ‘undefined’ group?

I’m trying to understand the .reduce() function and the best way to go about the following.

I’ve got the following code:

JavaScript

I want to add a product with no ‘category’ property in it, and I want it pushed into a specific key rather than getting grouped in “undefined”, so I edited it to:

JavaScript

For the most part it works (there’s still an ‘undefined’ group, but at least the object gets pushed into the right group).

I’m sure there’s a better solution/proper way to do this. Any pointers would be appreciated.

Advertisement

Answer

Instead of a whole new conditional block you could just set a default in the destructuring and then group as usual.

JavaScript

JavaScript

Note: you can also make use of logical nullish assignment (??=)

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement