javascript - Check for duplicates in an array -


i have function check serialized form data if there duplicates values in it.

    s = $('#multiselectform').serialize();     var x = [];     var y = [];     x = s.split("&");     (var = x.length - 1; >= 0; i--) {         y.push(x[i].split("="));     };     var c = 0;     var e = 0;     (var = y.length - 1; >= 0; i--) {         if (y[i][1] == y[c][1]) {             e++;             $('.duplicatealert').show();         } else {             $('.duplicatealert').hide();         };         c++;     }; 

basically, split string produced serialize() function , push data arrays.

the array i'm trying parse looks this:

array [     array [     0: 'my_field1',     1: 'val1'     ],      array [     0: 'my_field2'     1: 'val2'     ],      array [     0: 'my_field3'     1: 'val1'     ] ] 

are there better ways same task? maybe shorter?

  1. create empty array hold matches
  2. loop through array. on each iteration...
    1. loop through matches array , check if item same value exists. if does, set matched flag.
    2. check if matched flag has been set
      1. if so, alert user
      2. if not add item matches.

var array = [     [ 'my_field1', 'val1' ],     [ 'my_field2', 'val2' ],     [ 'my_field3', 'val1' ],     [ 'my_field4', 'val2' ],     [ 'my_field5', 'val3' ] ], matches = [], match = false;  for(var = 0, j = array.length; < j; i++) {     match = false;     for(var k = 0, l = matches.length; k < l; k++) {         if(matches[k][1] == array[i][1]) {             match = true;         }     }     if(match) alert('duplicate!');     else matches.push(array[i]); } 

Comments

Popular posts from this blog

html - How can i make an element from a bottom stacking context stays in front of another higher stacking context? -

python - TypeError: start must be a integer -

html - href attribute breaking an element -