Related Post Widget for Blogger - Eliminate Duplicates from Array

We have defined an approach for developing a related post widget for blogger. We have also crossed the first step in the process to identify all the labels for a given blog post.

We now have a 'labelarray' container which holds all the labels attached to the displayed blog posts in blogger. Since it is possible to have duplicate labels spanning across multiple blog posts, we are now in a situation to eliminate the duplicates from our label array. In order to eliminate duplicates from a Javascript array, we have a lot of defined approaches. The easiest one (time consuming as well) would be to loop through the array completely and get rid of the duplicates. I would leave this to the choice of the reader to eliminating this duplicate occurrence. The approach that I followed to eliminate the duplicates is presented below. [ Thanks to Stackoverflow for this piece ]

function eliminateDuplicates(arr) {
  var i,
  for (i=0;i<len;i++) {
  for (i in obj) {
  return out;

Note that we are defining this as a function, as we will have to make use of this more than once in our solution. This  Javascript function accepts an array of elements and removes the duplicate from it and returns back an Array. Having defined an function, we can now pass our labelArray to this function and remove the duplicates from it.

//eliminate duplicates from label array
var filtArray=eliminateDuplicates(labelArray);

At the end of this step, we will have an array, 'filtArray' which would hold the list of labels, ignoring the duplicates. We are now ready to move to the next step, i.e. to use the Google Feed API and get the relevant posts.

No comments:

Post a Comment