简体   繁体   中英

Join Array of Objects by Property using lodash

Is there a way using lodash or another library to join an array of objects?

I'm looking for a readymade function not a for loop.

For example:

[{a: 1}, {a:3}, {a: 4}]
      //Run a function by specifing the property a and setting "," as the delimeter
Get 1,3,4

Here is your lodash answer

var arr = [{a: 1}, {a:3}, {a: 4}];
var s = _.map(arr, 'a').join(',');
//s == '1,2,3,4'

You don't need lodash for this, you can just use map and join :

 let collection = [{a: 1}, {a:3}, {a: 4}]; alert(collection.map(item => item.a).join(',')); 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM