[英]Underscore.js - get unique property values
I only recently discovered the power of underscore.js, still new to the methods I kindly ask for a suggestion: 我最近才发现了underscore.js的强大功能,对我提出建议的方法仍然是新手:
How do I get from this: 我如何从中得到:
[
[{
"name": "Type 2",
"id": 14
}],
[{
"name": "Type 1",
"id": 13
}, {
"name": "Type 3",
"id": 15
}],
[{
"name": "Type 2",
"id": 14
}],
[{
"name": "Type 1",
"id": 13
}]
]
to this: 对此:
["Type 1","Type 2","Type 3"]
ie no duplicated and "name" property only. 即没有重复和“名称”属性。
Any suggestions much appreciated. 任何建议非常感谢。
_(data).chain().flatten().pluck('name').unique().value()
(将嵌套列表转换为平面列表,从列表中的每个对象中选择name
,并使其唯一。)
var arr = _.uniq(_.map(_.flatten(array), function(e) {
return e.name;
}));
_.uniq(_.pluck(x,'name'));
上面的代码足以提取不同的“名称”属性
Simple way: 简单方法:
1. use _.map to get all the names 1.使用_.map获取所有名称
var names = _.map(items, function(item) { return item.name});
2. Get the _.uniq from that names 2.从这些名称中获取_.uniq
var uniqueNames = _.uniq(names);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.