0

How to do two variables in an if condition? Here I have few else ifs and I want a 100 else ifs! Is there a shorter way?

$(document).on('click', '.btn-next', function () {
    var z = [];
    var recipientsArray = z.sort();
    var zDuplicate = [];
    $('option:selected.exclude_global').each(function() {
    z.push($(this).val())});
    for (var i = 0; i < recipientsArray.length - 1; i++) {
    if (recipientsArray[i + 1] == recipientsArray[i]) {
    zDuplicate.push(recipientsArray[i]);
    }else if(recipientsArray[i + 2] == recipientsArray[i]){
    zDuplicate.push(recipientsArray[i]);
    }else if(recipientsArray[i + 3] == recipientsArray[i]){
    zDuplicate.push(recipientsArray[i]);
    }else if(recipientsArray[i + 4] == recipientsArray[i]){
    zDuplicate.push(recipientsArray[i]);
    }

    }
    if(zDuplicate.length>>0){
        alert("Global Filter Already Exists");
        event.preventDefault();
    }
});

Here I have few else ifs and I want a 100 else ifs! Is there a shorter way? I have a dynamic table with dynamic rows. when my table has 5 rows the code is working, but when I have more its not working.

Mark
  • 2,380
  • 11
  • 29
  • 49
Chris Jake
  • 59
  • 1
  • 9
  • You're already looping over each of these elements. Why do you need to check all 100 of them every time you iterate through each element? – Bucket Aug 05 '15 at 19:13
  • 1
    Can you give a better indication of exactly what you're trying to do? It looks like what you're really trying to accomplish is to detect duplicate entries in the `recipientsArray`, is that correct? – siva.k Aug 05 '15 at 19:14
  • Take a look here: [Easiest way to find duplicate values in a JavaScript array](http://stackoverflow.com/questions/840781/easiest-way-to-find-duplicate-values-in-a-javascript-array) – blex Aug 05 '15 at 19:15
  • And this is not Java! – laune Aug 05 '15 at 19:16

3 Answers3

2

What you're looking for is called a nested loop. You basically can write a loop within a loop. (As many as you want, actually. Though it can get ugly fast.)

Consider your loop structure:

for (var i = 0; i < recipientsArray.length - 1; i++) {
    // check if recipientsArray[i] equals any other element
}

Well, that's just another loop:

for (var i = 0; i < recipientsArray.length - 1; i++) {
    for (var j = i + 1; j < recipientsArray.length - 1; j++) {
        if (recipientsArray[j] == recipientsArray[i]) {
            zDuplicate.push(recipientsArray[i]);
            break;
        }
    }
}

Note that there's probably a more efficient way of checking for duplicates. (Especially if the collection is sorted.) At the very least I've changed the logic so you're not re-comparing comparisons you've already made. (I did this by starting the inner loop at i + 1 instead of 1 as your logic does.)

I also think I've replicated your else if results by using a break statement. Since your else if logic basically means "once you find one, stop looking". That's what this break should do, or at least is intended to do, but you'll want to test that. If it doesn't (nesting can be confusing sometimes, which is why it should be done carefully) then you can probably make use of labels to achieve the same effect.


Ultimately, however you implement it, the concept is the same. You're asking how to iterate over multiple values in an array. That's what a loop is for.

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
2

I don't know that language. But data and control structures are data and control structures in any language.

Substitute your for loop by :

for (var i = 0; i < recipientsArray.length - 1; i++) {
  for ( var j = i+1; j < recipientsArray.lenght-1; j++) {
    if (recipientsArray[i] == recipientsArray[j]) {
      zDuplicate.push(recipientsArray[i]);
      break;
    }
  }
}
Anonymous Coward
  • 3,140
  • 22
  • 39
1

Thank you so much for the idea!

A small change to your code works like heaven!

for (var i = 0; i < recipientsArray.length - 1; i++) {
                    for (var j = 1; j < 100; j++) {
                        if (recipientsArray[i+j] == recipientsArray[i]) {
                            zDuplicate.push(recipientsArray[i]);
                            break;
                        }
                    }
                }
Chris Jake
  • 59
  • 1
  • 9