繁体   English   中英

使用lodash按属性连接对象数组

[英]Join Array of Objects by Property using lodash

有没有一种方法可以使用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

这是你的回答

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

您不需要lodash,只需使用mapjoin

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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