0

I am collecting a textbox value from a table rows. The value of textbox is varies. I'm trying to remove the duplicate from the string. I'm looking at this accepted answer but the duplicate is still exist.

var memo    = "";
$("#f_sendToPic").find('#t_obj input[class=no_memo]').each(function(index, result) {
   memo +=$(this).val();
   memo += "@";
});

var val_memo = $.unique(memo.split('@'));
var arr_memo = val_memo.join('|||');

alert(memo);
alert(arr_memo);

I prefer browser friendly method than performance , if it's possible.

Vahn
  • 542
  • 1
  • 10
  • 25
  • as an `@` a valid entry in the text boxes? if so you wouldn't be able to use it as a delimiter. – t3dodson Jul 17 '17 at 03:46
  • 1
    Also from the [docs](https://api.jquery.com/jQuery.unique/) `Description: Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers.` You need to call unique on dom elements, not strings version added: 1.1.3 – t3dodson Jul 17 '17 at 03:48
  • @t3dodson no. I prevent special chars to be inputted to the textbox. So, the text box value maybe string, number, or combination. – Vahn Jul 17 '17 at 03:48
  • 1
    Why do you append it to string and then split it again. Why don't you collect its vals into an array. – Yeldar Kurmangaliyev Jul 17 '17 at 03:50
  • To clarify, you're talking about removing duplicate *words* or values, not duplicate *characters*, right? – nnnnnn Jul 17 '17 at 03:51
  • @YeldarKurmangaliyev I am going to save the string in a table. – Vahn Jul 17 '17 at 03:51
  • @nnnnnn yes. The words between `@` need to be removed if it a duplicate. – Vahn Jul 17 '17 at 03:52
  • 1
    Yeldar means, why dont you just do `arr_memo.push($(this).val())` inside your `.each` then do `var arr_memo = val_memo.join('|||');` after the loop, the whole part with joining the values with `@` then immediately splitting it on `@` seems pointless – Wesley Smith Jul 17 '17 at 03:54
  • @DelightedD0D is that method removing the duplicates? – Vahn Jul 17 '17 at 03:57
  • No, nnnnn hs you covered below with that part. We just didnt understand why you were building a string just to spilt it right back apart again. – Wesley Smith Jul 17 '17 at 03:59

1 Answers1

2

Use an object with properties names set to the values and duplicates will automatically be filtered out. Then use Object.keys() to get the property names in an array and .join() the array to get a string with values separated by "@".

var working = {};
$("#f_sendToPic").find('#t_obj input[class=no_memo]').each(function() {
  working[this.value] = true;
});

var memo = Object.keys(working).join("@");

console.log(memo);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="f_sendToPic"><div id="t_obj">
  <input class="no_memo" value="abc">
  <input class="no_memo" value="def">
  <input class="no_memo" value="abc">
  <input class="no_memo" value="def">
  <input class="no_memo" value="ghi">
</div></div>

As an aside, note that this.value is easier to read, easier to type, and much more efficient to execute than $(this).val();.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • thank you for your answer. I get the expected output. – Vahn Jul 17 '17 at 04:00
  • 2
    You can also use `Set` instead of object and `Object.keys`, if your code can be ES6+. It is more suitable and looks better, but is not supported by all browsers yet. – Yeldar Kurmangaliyev Jul 17 '17 at 04:10
  • @YeldarKurmangaliyev - If the code in a question looks old-school I generally don't suggest new language features in an answer, and as you know `Set` isn't supported by IE (partial support in IE11 doesn't count). But yes, `Set` would do the same thing more neatly. – nnnnnn Jul 17 '17 at 04:14
  • @nnnnnn Yeah, I have understood it too, so I have updated my answer :) – Yeldar Kurmangaliyev Jul 17 '17 at 04:15