Reputation: 6176
The way I'm doing it is to save that properties into an array and after that to join them.
Original array of objects:
const objArray = [ { prop: "a", etc: 1}, { prop: "b", etc: 2}, { prop: "c", etc: 3} ];
First step, save the values of property prop
into an array:
const firstStep = objArray.map(a => a.prop);
Second step, concatenate them into a string:
const secondStep = firstStep.join(' + ');
This works fine but I'm thinking if there is a better/shorter method to do this. Any ideas?
Upvotes: 0
Views: 47
Reputation: 370759
It isn't much, but you can chain the operations together, there's no need to save the intermediate result in a variable first:
const result = objArray
.map(a => a.prop)
.join(' + ');
const objArray = [ { prop: "a", etc: 1}, { prop: "b", etc: 2}, { prop: "c", etc: 3} ];
const result = objArray
.map(a => a.prop)
.join(' + ');
console.log(result);
Other than that, I don't think it's possible to implement the logic in a shorter fashion.
Upvotes: 1