0

I have an array of data returned from my server. From this array I need to get an array of Topics and an array of 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"}
]

I have code that I use to reformat this data and just give me topic information:

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

But this gives me three entries for topicId 1 and six entries for topidId 2.

How I can filter out duplicate entries so I can for example the above would just give me a topic array of two entries. One for each topicId

Please no jQuery, lodash or other framework solutions as I didn't include these in the tags. thanks

4 Answers 4

1

This should work

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
    };
});

If duplicate entries are equal, you can simplify filter function

data.filter(function(item, index, data) {   
   return data.indexOf(item) === index;
})
Sign up to request clarification or add additional context in comments.

Comments

0

Here is the solution:

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);

This will print the following into console:

enter image description here

Comments

0

Try this

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

unique will have unique array of topics.

Comments

0

I suggest to use the smart library underscore.js http://underscorejs.org/

They implement functional behaviors like groupBy http://underscorejs.org/#groupBy

So you can simply use _.groupBy(data, function(t){return t.topicId;}) and you get grouped 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"
    }, ... ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.