2016-07-28 150 views
0

有沒有使用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 

回答

4

你並不需要爲這個lodash,你可以只使用mapjoin

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

+0

hehe ecma script ...幸運的人,因爲你被允許使用它=) –

+0

@java_newbie你是絕對正確的,也許你可以讓你的團隊使用一個翻譯和一些polyfills - 值得頭痛! –

4

這裏是你的答案lodash

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