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
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
Share
Improve this question
asked Jul 28, 2016 at 23:15
KingKongFrogKingKongFrog
14.4k22 gold badges76 silver badges131 bronze badges
2 Answers
Reset to default 10Here 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(','));