I have an array of objects. The objects have a property called userName
. Is there a way to concatenate the userName
values into a comma delimited string? I assume I can use the join
function but the only way I can think of takes two steps.
var userNames: string[]; objectArr.forEach((o) => { userNames.push(o.userName); }); var userNamesJoined = userNames.join(",");
Is there a way to do it in one line of code?
Use map
instead of forEach
and drop the parenthesis and the curly braces in the lambda:
var userNames = objectArr.map(o => o.userName).join(', ');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With