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

Estimated read time 2 min read

To perform a .join() operation on a specific value in an array of objects in JavaScript, you can use the map() and join() methods together. The map() method can be used to extract the specific value from each object in the array, and the join() method can be used to concatenate the resulting array of values into a single string using a specified delimiter. Here’s an example:

const items = [
  { name: 'apple', price: 0.5 },
  { name: 'banana', price: 0.25 },
  { name: 'cherry', price: 1.0 }
];

const prices = items.map(item => item.price);
const joined = prices.join(', ');

console.log(joined);
// Output: '0.5, 0.25, 1'

In this example, the items array contains objects representing different fruit and their prices. The map() method is used to extract the price property from each object in the array and create a new array of just the prices. The join() method is then used to concatenate the resulting array of prices into a single string, with each price separated by a comma and a space. The resulting joined string contains all the prices in the array concatenated together with a delimiter.

Note that the map() method can be used to extract any property from each object in an array, not just the price property in this example.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply