我有一组对象
var array =
[
{"category":"A", "categoryname":"somename", key: 1, description: "something"},
{"category":"A", "categoryname":"somename", key: 1, description: "something"},
{"category":"B", "categoryname": "somename", key: 1, description: "something"}
{"category":"B", "categoryname": "somename", key: 3, description: "something"}
{"category":"C", "categoryname": "somename", key: 2, description: "something"}
{"category":"C", "categoryname": "somename", key: 2, description: "something"}
]
有4个值(类别,类别名称,键,描述),我需要遍历它并根据类别获取唯一的对象列表。但我希望返回2个preoperties的唯一列表,而不只是一个。如果我使用
_.uniq(_.pluck(array,"category"))
这给了我想要的东西,除了我需要categoryname和类别。这可能吗?
答案 0 :(得分:2)
对于同一个用例已经有几个问题。
答案 1 :(得分:-1)
您可以使用$.unique()
,Array.prototype.map()
,JSON.stringify()
,JSON.parse()
var array = [{
"category": "A",
"categoryname": "somename",
key: 1,
description: "something"
}, {
"category": "A",
"categoryname": "somename",
key: 1,
description: "something"
}, {
"category": "B",
"categoryname": "somename",
key: 1,
description: "something"
}, {
"category": "B",
"categoryname": "somename",
key: 3,
description: "something"
}, {
"category": "C",
"categoryname": "somename",
key: 2,
description: "something"
}, {
"category": "C",
"categoryname": "somename",
key: 2,
description: "something"
}];
var res = $.unique(array.map(function(el) {
return JSON.stringify([el.category, el.categoryname])
})).map(JSON.parse);
console.log(res);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>