Categories
JavaScript Answers

How to perform .join on value in array of objects with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.