admin管理员组

文章数量:1245083

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
Add a ment  | 

2 Answers 2

Reset to default 10

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(','));

本文标签: javascriptJoin Array of Objects by Property using lodashStack Overflow