0

I have an array with different ID's. This array is populated from query, so number of ID's can be different each time. So I want to check if my eventID exist in that array, if eventID has matching ID in array I want to alert message, if not I want to submit my form. My problem is when eventID does not match any record in array my form does not get submitted. Here is my code:

 function saveSlots(){
                var records = [];
                var eventID = '#eventID#';
                //here is my array
                records.push(55,56,78,90,44); 

                for(var i=0; i< records.length; i++){
                    if(records[i] == eventID){
                        alert('duplicate')
                    }else{
                        //Submit the form
                    }
                }   

            }

For some reason my else never get executed. In my if statement I just need to check if one of the records from array is matching eventID and if does I do not need to check for other ID's in array. If anyone can help with this problem please let me know.

espresso_coffee
  • 5,980
  • 11
  • 83
  • 193
  • 1
    Possible duplicate of [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) – Artyom Neustroev Feb 17 '16 at 20:16
  • Your form doesn't submit? You should show that code then instead of `// Submit the form` ... Your else will work properly, but who knows what you're actually doing there. – Eric Hotinger Feb 17 '16 at 20:17
  • 1
    @ArtyomNeustroev I disagree. This is not a duplicate. The issue is not with how to find duplicate values, but with the logic used. – nderscore Feb 17 '16 at 20:17
  • 1
    You can’t do that with a simple if-else in within the loop – you need to check _all_ values, but if you submitted the form in the else branch, that would happen after the _first_ array value was not found to be the wanted one. You’ll need to set a flag inside your loop if the value was found (and then break out of the loop), and check the value of that flag after the loop to decide what to do. – CBroe Feb 17 '16 at 20:18
  • I had an alert in my else statement and that never was executed. That's why I have not put submit form code. – espresso_coffee Feb 17 '16 at 20:18
  • Did you check the browser console for errors? And how are you calling this function – when the form is submitted by the user? Show us how exactly that happens. – CBroe Feb 17 '16 at 20:19
  • @eh Adding the submitting of the form code would just overly complicate the code example. It is not necessary for the example; and the OP would be correct to not included it. – Josh Mein Feb 17 '16 at 20:19
  • @user3023588 the only way it will not execute is if you have 0 records or the case is always true (full of duplicates)... – Eric Hotinger Feb 17 '16 at 20:20
  • Can anyone provide simple example that will execute alert in else statement? – espresso_coffee Feb 17 '16 at 20:21
  • 1
    @JoshMein I disagree. If he's submitting it without breaking after the first match or isn't submitting correctly then it is a flaw we will not know about. – Eric Hotinger Feb 17 '16 at 20:21
  • I'd use the default array.some() function for simple objects like that. If one (or more) are found, it'll return true. No icky loops needed. – MartijnK Feb 17 '16 at 20:43
  • @eh As the OP said he placed an alert in the else and it was never even called. So that should not be the problem. – Josh Mein Feb 17 '16 at 21:58

2 Answers2

1

The way your code is written right now, you'll be submitting the form once for every element that doesn't match, since your conditional if-else will be checked for each item in the array.

I would recommend using a variable to flag that a duplicate was found. Then, after your loop finishes check that variable and decide what to do next:

function saveSlots() {
    var records = [];
    var eventID = '#eventID#';
    //here is my array
    records.push(55, 56, 78, 90, 44);

    var foundDuplicate = false;

    for (var i = 0; i < records.length; i++) {
        if (records[i] == eventID) {
            foundDuplicate = true;
        }
    }

    if(foundDuplicate) {
        alert('Duplicate found!');
    } else {
        alert('Submit form!');
    }
}

Alternatively, you could return to break out of the function as soon as you find a duplicate:

function saveSlots() {
    var records = [];
    var eventID = '#eventID#';
    //here is my array
    records.push(55, 56, 78, 90, 44);

    var foundDuplicate = false;

    for (var i = 0; i < records.length; i++) {
        if (records[i] == eventID) {
            alert('Duplicate found!');
            return;
        }
    }

    alert('Submit form!');
}
nderscore
  • 4,182
  • 22
  • 29
  • I used your first answer and works but nor fully. Problem is if I do not have matching ID and try to submit my form, I can submit multiple times. But if I come after that logic works fine and alert message pops up. How I can prevent submitting multiple time if I do not have matching record first time? – espresso_coffee Feb 17 '16 at 20:35
  • @user3023588 The `foundDuplicate` variable would be reset each time the function is called, so it shouldn't be an issue? Not sure if I'm understanding completely. – nderscore Feb 17 '16 at 20:56
  • If I do not have duplicate in my array and I want to submit my form I will be able to submit more than once even if that will produce duplicate records in db. Only If I refresh the page then logic will throw alert with the message that record already exist. – espresso_coffee Feb 17 '16 at 21:09
  • @user3023588 So you'd need to add the new item to your array after submitting the form? – nderscore Feb 17 '16 at 21:30
  • Yes that should prevent submitting duplicates. – espresso_coffee Feb 17 '16 at 22:00
1

There are a couple things that could be preventing you.

  1. Make sure to confirm your event id. You have a string '#eventID#' which in javascript will be fine it will still evaluate '55' to 55 so you are ok but make sure you look at the number to see if it is correct.

  2. Your loop is going to call your submit multiple times because your looping through it. In other words 60 is not in your array so when it is in the for loop looking at the first value of 55 it does not find it so it submits, then when it does not match 56 it submits again. You should put a break; in the submission code to stop the for loop from continuing if you reach submit. These multiple submits might be causing issues. Even better use index of to look it up. indexOf will return -1 if it is not found so

    function saveSlots(){ var records = []; var eventID = '60'; records.push(55,56,78,90,44); if(records.indexOf(eventID) === -1) { console.log("submission reached"); } }

  3. You should confirm that your submission is working properly, you may find that the console.log is being executed, which means the javascript is reaching the correct area but the submission does not seem to work, in this case you know the javascript is fine but there is an issue with your submission.

thawts
  • 71
  • 6