使用lodash根据属性连接对象数组

3

有没有一种使用lodash或其他库将对象数组连接起来的方法?

我正在寻找一个现成的函数,而不是一个for循环。

例如:

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

10
这里是您的 lodash 答案。
var arr = [{a: 1}, {a:3}, {a: 4}];
var s = _.map(arr, 'a').join(',');
//s == '1,2,3,4'

6
您无需使用lodash,可以直接使用mapjoin来完成此操作:

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


哈哈,ECMAScript...真幸运啊,你可以使用它 =) - java_newbie
@java_newbie 你说得太对了,也许你可以让你的团队使用一个转译器和一些填充物 - 这值得一番努力! - Rob M.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接