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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.