如何过滤数组以删除重复的条目?

时间:2014-11-05 10:52:36

标签: javascript

我有一个从我的服务器返回的数据数组。从这个数组我需要 获取一系列主题和一系列SubTopics:

var data = 
[
{"topicId":1,"subTopicId":1,"topicName":"J","subTopicName":" Ar"},
{"topicId":1,"subTopicId":2,"topicName":"J","subTopicName":" Us"},
{"topicId":1,"subTopicId":3,"topicName":"J","subTopicName":" Ut"},
{"topicId":2,"subTopicId":4,"topicName":"L","subTopicName":" Ov"},
{"topicId":2,"subTopicId":5,"topicName":"L","subTopicName":" El"},
{"topicId":2,"subTopicId":6,"topicName":"L","subTopicName":" In"},
{"topicId":2,"subTopicId":7,"topicName":"L","subTopicName":" Pr"},
{"topicId":2,"subTopicId":8,"topicName":"L","subTopicName":" Va"},
{"topicId":2,"subTopicId":9,"topicName":"L","subTopicName":" Pa"}
]

我有用于重新格式化此数据的代码,只是给我主题信息:

var topics = data.map(function (t) {
                    return {
                        id: t.topicId, name: t.topicName
                    };
                });

但是这为topidId 2的topicId 1和 6 条目提供了 3个条目。

我如何过滤掉重复的条目,所以我可以举例说明上面只会给我一个包含两个条目的主题数组。每个topicId一个

请不要使用jQuery,lodash或其他框架解决方案,因为我没有在标签中包含这些解决方案。感谢

4 个答案:

答案 0 :(得分:1)

这应该有效

topics = data.filter(function(item, index, data) {
   for (var i = 0; i < data.length; i++) {
      if (item.topicId === data[i].topicId) break;
   }

   return index === i;
}).map(function (item) {
    return {
        id: item.topicId,
        name: item.topicName
    };
});

如果重复条目相同,则可以简化过滤功能

data.filter(function(item, index, data) {   
   return data.indexOf(item) === index;
})

答案 1 :(得分:0)

以下是解决方案:

var data = 
[
{"topicId":1,"subTopicId":1,"topicName":"J","subTopicName":" Ar"},
{"topicId":1,"subTopicId":2,"topicName":"J","subTopicName":" Us"},
{"topicId":1,"subTopicId":3,"topicName":"J","subTopicName":" Ut"},
{"topicId":2,"subTopicId":4,"topicName":"L","subTopicName":" Ov"},
{"topicId":2,"subTopicId":5,"topicName":"L","subTopicName":" El"},
{"topicId":2,"subTopicId":6,"topicName":"L","subTopicName":" In"},
{"topicId":2,"subTopicId":7,"topicName":"L","subTopicName":" Pr"},
{"topicId":2,"subTopicId":8,"topicName":"L","subTopicName":" Va"},
{"topicId":2,"subTopicId":9,"topicName":"L","subTopicName":" Pa"}
]

var arr = [],
    collection = [];

$.each(data, function (index, value) {
    if ($.inArray(value.topicId, arr) == -1) {
        arr.push(value.topicId);
        collection.push(value);
    }
});
console.log(collection);

这会将以下内容打印到控制台:

enter image description here

答案 2 :(得分:0)

试试这个

var topicIds = {};
var unique = [];
topics.forEach(function(t){ 
      if(!topicIds[t.id]){ 
          unique.push(t); 
          topicIds[t.id] = true; 
      } 
});

unique将包含一系列独特的主题。

答案 3 :(得分:0)

我建议使用智能库underscore.js http://underscorejs.org/

他们实现了groupBy http://underscorejs.org/#groupBy

等功能行为

所以你可以简单地使用_.groupBy(data, function(t){return t.topicId;})并获得分组的suptopics:

Object {1: Array[3], 2: Array[6]}
1: Array[3]
    [{
        subTopicId: 1
        subTopicName: " Ar"
        topicId: 1
        topicName: "J"
    },{
        subTopicId: 2
        subTopicName: " Us"
        topicId: 1
        topicName: "J"
    }, ... ]