Skip to content Skip to sidebar Skip to footer

Javascript Remove Duplicates From Text/array

I want to remove lines in text based on duplicate keywords. The text goes something like this. text1,abc,text2 text3,cde,text4 text5,abc,text6 text7,gfh,text8 text9,cde,text10 I w

Solution 1:

Here's a jsFiddle that will remove duplicates in the middle column of your data array: http://jsfiddle.net/bonneville/xv90ypgf/

var sortedAr = ar.sort(sortOnMiddleText);
var noDupsAr = removeDups(sortedAr);

Firstly is sorts on the middle column, and then it removes the duplicates. The code is pure javascript but uses jQuery to display the results. (I would prefer to use javaScript forEach instead of the for loop but it is not supported on all browsers so you would need a polyfill.)

Solution 2:

The best way to resolve it is to use a key_value array (key-value pair), and note that the key is unique, so you will get automatically a new table with unique values. Hope this will help you.

function RemoveDuplicate(table_data) {
      var listFinale = [], key_value = {};
      for (var i = 0; i < table_data.length; i++) {
        key_value[table_data[i]] = i;
      }
      for (var key in key_value) {
        listFinale.push(key);
      }
      return  listFinale.join(",");
    }

Post a Comment for "Javascript Remove Duplicates From Text/array"