使用lodash按属性加入对象数组

时间:2021-08-02 19:39:30

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

有没有办法使用lodash或其他库来加入一个对象数组?

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

我正在寻找一个现成的功能而不是for循环。

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

2 个解决方案

#1


4  

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

你不需要lodash,你可以使用map和join:

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

#2


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'

#1


4  

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

你不需要lodash,你可以使用map和join:

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

#2


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'