简体   繁体   English

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

[英]Join Array of Objects by Property using lodash

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

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 : 您不需要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