[英]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,只需使用map
和join
:
let collection = [{a: 1}, {a:3}, {a: 4}]; alert(collection.map(item => item.a).join(','));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.