0

I have an array of object

 const arr = [
  { id: 1, value: "Apple" },
  { id: 1, value: "Orange" },
  { id: 1, value: "Pine Apple" },
  { id: 1, value: "Banana" },
];

I want to loop through this array and get all the fruit names as a single string every fruit will separated with comma. But don't know how to do that. Does anybody help me to do this?

romjan
  • 75
  • 1
  • 9

2 Answers2

4

You can use map and join like this

const arr = [
  { id: 1, value: "Apple" },
  { id: 1, value: "Orange" },
  { id: 1, value: "Pine Apple" },
  { id: 1, value: "Banana" },
];
const result = arr.map(({ value }) => value).join(', ')
console.log(result)
Cuong Vu
  • 3,423
  • 14
  • 16
0
const fruitNames = arr.map(fruit => fruit.value).toString();
milan
  • 235
  • 2
  • 13