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?
CodePudding user response:
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)
CodePudding user response:
const fruitNames = arr.map(fruit => fruit.value).toString();