0

I created this function to find duplicate entries in a web form, what I need is to store the index numbers of each duplicated item in another array..to

function arrTieneDups( arr ) 
{                          
    var xi, xj, xn;
    n=arr.length;

    for (xi=0; xi<n; xi++) {                        
        for (xj=xi+1; xj<n; xj++) {              
            if (arr[xi]==arr[xj]) return true;
    }   }
    return false;
}

loop through that new array and use it as well..

for (var i in arr2)
{
    $( "input[name$='matricula"+i+"']" ).addClass("errorformalta");
    $( "input[name$='matricula"+i+"']" ).addClass("errorinputtext"); 
    $( "input[name$='matricula"+i+"']" ).after('<span style="display: inline; margin-left: -20px; opacity: 1;font-size:48px;color:red;font-weight:800;" class="pyr_error_form">!</span>');
}

I am now doing as well..through the Orcuro code. But the result is not as expected. I need "var it" to be the index number of repeated items. e.g: 2,4,6 What I want is, indicate to the user the fields where they have introduced repeated data.

for (var it in arrTieneDups(matriculas) )
{
  $( "input[name$='matricula"+it+"']" ).addClass("errorformalta");
  $( "input[name$='matricula"+it+"']" ).addClass("errorinputtext"); 
  $( "input[name$='matricula"+it+"']" ).after('<span style="display: inline; margin-left: -20px; opacity: 1;font-size:48px;color:red;font-weight:800;" class="pyr_error_form">!</span>');
  error_validation=1;
  error_top=1;
  error_mat_formato="</br>! Matrícula(s) inválida(s)";
  error_look="</br>Revise también los siguientes errores:";
  all_ok=1;
}
3
  • you can try using map: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Dec 1, 2015 at 16:03
  • "index numbers of each duplicated item" - just the index of duplicate, or the indces of the original and it's duplicate.? Please provide sample input and output. Commented Dec 1, 2015 at 16:10
  • Thanks for your help! I need the index numbers of each duplicated item. Commented Dec 1, 2015 at 16:38

2 Answers 2

1

You could pass this "duplicate object" array as a parameter. Something like...

 function arrTieneDups( arr ) 
 {                          
    var xi, xj;
    var dupeArray = [];
    n=arr.length;

    for (xi=0; xi<n; xi++) {                        
        for (xj=xi+1; xj<n; xj++) {              
            if (arr[xi]==arr[xj]) 
                dupeArray.push(arr [xi]);
    }   }
    return dupeArray;
}

From there, outside your function, iterate over this array and add the necessary styles as you were before.

for (var i in  arrTieneDups( yourArray ) )
{
    $( "input[name$='matricula"+i+"']" ).addClass("errorformalta");
    $( "input[name$='matricula"+i+"']" ).addClass("errorinputtext"); 
    $( "input[name$='matricula"+i+"']" ).after('<span style="display: inline; margin-left: -20px; opacity: 1;font-size:48px;color:red;font-weight:800;" class="pyr_error_form">!</span>');
}

Syntax might be off. I have no way of testing right now.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your input code. But .. How do I call the function ?. Before I was doing so, if ( arrTieneDups( matriculas ) ) .So, the function was called when it found duplicates.
I would call the function within the for() loop parameters. The loop won't iterate if the array returned from arrTieneDups is empty. I've altered my code to closer-reflect your original design - arrays holding indexes. I hope it's at least somewhat helpful. However, I would still be inclined, since you are using jQuery already, to add the actual objects to an array and alter them that way. Without seeing how you get your original arr that you pass to your function, I can't really advise how to go about this.
1

I would suggest instead of returning true when finding a duplicate to simply store that index (xi in your case) to duplicates array and after the loop return that array.

also variable xn is not used anywhere from what i see here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.