To perform .join on value in array of objects with JavaScript, we use map
to map the array of object to an array of primitive values.
For instance, we write
const users = [
{ name: "Joe", age: 22 },
{ name: "Kevin", age: 24 },
{ name: "Peter", age: 21 },
];
const s = users.map((u) => u.name).join(", ");
to call users.map
with a callback that gets the name
property value from each object and return them in an array.
Then we call join
to combine then into a string with commas.