我试图使用loadsh从以下数组中获取唯一类别,
[{
"listingId": "p106a904a-b8c6-4d2d-a364-0d21e3505010",
"section": "section1",
"category": "VIP PASS ",
"type": "paper",
"availableTickets": 1
}, {
"listingId": "p106904a-b8c6-4d2d-a364-0d21e3505010",
"section": "section1",
"category": "VIP PASS ",
"type": "paper",
"availableTickets": 2
}, {
"listingId": "pc8f54389-4e58-482a-9535-6917c2948764",
"section": "1",
"category": "WIP",
"type": "paper",
"availableTickets": 1
}]
这就是我尝试过的
this.categories = _.uniq(this.listings, function (test: ListDAO) { return test.category; });
但是上面再次返回相同的数组.我怎样才能得到输出结果,
VIP PASS和WIP
解决方法:
没有lodash并使用.reduce:
let arr2 = arr.reduce((a, i) => a.indexOf(i.category) > -1 ? a : a.concat(i.category), []);