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 want to make it into.
text1,abc,text2
text3,cde,text4
text7,gfh,text8
The idea is to take the text, split it based on lines and put it through 2 loops. Then comparing the two arrays it would remove duplicates from it. How can I do this?
-
Possible duplicate of stackoverflow.com/questions/9229645/…soktinpk– soktinpk2014年11月09日 19:57:09 +00:00Commented Nov 9, 2014 at 19:57
-
1The idea sounds good, so what went wrong? Where did you get stuck?David Thomas– David Thomas2014年11月09日 20:01:02 +00:00Commented Nov 9, 2014 at 20:01
-
I wrote some code but didn't work. It splits the text, starts comparing but doesn't do a good job.edinvnode– edinvnode2014年11月09日 20:13:22 +00:00Commented Nov 9, 2014 at 20:13
-
And this is not a duplicate from that question.It's different.edinvnode– edinvnode2014年11月09日 20:30:18 +00:00Commented Nov 9, 2014 at 20:30
-
You haven't specified on what criteria a line would be considered a duplicate - I had to guess. Also, you haven't shown what you've done so far and why the answer @DavidThomas linked wasn't adequate.Tom W– Tom W2014年11月09日 21:44:10 +00:00Commented Nov 9, 2014 at 21:44
2 Answers 2
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.)
2 Comments
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(",");
}