【发布时间】:2018-07-02 13:24:37
【问题描述】:
我正在尝试找到一种方法来转换 JS 对象上的主题列表,以便我可以创建一个没有重复值的过滤器。
到目前为止,我设法将唯一值映射并过滤到两个单独的数组中。我设法编写的代码是基本的(并没有解决问题):
var topic = subject
.map(function (value) { return value.topic })
.filter(function (elem, index, self) {
return index == self.indexOf(elem);
});
所以我的主题的形状是这样的:
var subjects = [ {
"topic" : "Social Sciences",
"subtopic" : "Developmental Issues"
}, {
"topic" : "Social Sciences",
"subtopic" : "General"
}, {
"topic" : "Social Sciences",
"subtopic" : "General"
}, {
"topic" : "Social Sciences",
"subtopic" : "General and Others"
},{
"topic" : "Social Sciences",
"subtopic" : "Arts"
},{
"topic" : "Social Sciences",
"subtopic" : "History"
}, {
"topic" : "Arts and Humanities",
"subtopic" : "History"
}, {
"topic" : "Arts and Humanities",
"subtopic" : "Literature"
} ]
我需要创建一个如下所示的过滤器:
filter = [{
name: "Social Sciences",
{
subtopic: "Developmental Issues",
subtopic: "General",
subtopic: "General and Others",
subtopic: "Arts",
subtopic: "History"
}
}, {
name: "Arts and Humanities",
{
subtopic: "History",
subtopic: "Literature"
}
}
【问题讨论】:
标签: javascript arrays object filter