6

You could do:

let categorysString = '';
categories.map((item, index) => {
if ( index === 0 ) {
categorysString = categorysString + `${item.categoryName}`;
} else {
categorysString = categorysString + `, ${item.categoryName}`;
}
});

Or you could just do:

return categories.map(category => category.categoryName).join(", ")

🙄

Previous company must have been payed per line...

Comments
  • 1
    Many coders don't know of these array methods (filter(), map(), reduce()). I was doing this in the early stages of my career as well...
  • 3
    also why bother to add 'categoryString' when index == 0...

    You already know it is the first element and the string is empty no matter what.
  • 3
    The join function is certainly one of the best examples of the simplification power of functional coding.
  • 0
    @grumm because of the comma.
Add Comment