有没有一种使用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
有没有一种使用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'
map
和join
来完成此操作:
let collection = [{a: 1}, {a:3}, {a: 4}];
alert(collection.map(item => item.a).join(','));