Perform .join on value in array of objects
To obtain a string of concatenated property values from an array of objects, .map() and .join() come to your rescue. Here's the prime cut:
Map & join… Batman & Robin!
Much like Batman and Robin make a dynamic duo, the .map() and .join() functions combine to form a powerful team in JavaScript.
Arrow functions, saving the day
Our superhero, the ECMAScript 6 arrow function, swoops in to inject readability and brevity into the array callbacks:
Nested properties, you’ve met your match
Handling nested properties? Fear not. Destructuring gives you access:
Old browsers, we’ve got you
For older browsers, utilize a polyfill for the .map()
function. The MDN documentation provides solid options.
Reduce — an undercover alternative
.reduce()
—a sneaky superhero in disguise, can operate alternatively to .map().join()
, eliminating intermediate arrays:
Custom join behavior — your secret weapon
Control the .join()
behavior by declaring a custom toString
within your array objects:
Was this article helpful?