简体   繁体   English

如何检查元素是否存在于数组中?

[英]How to check if element exist in array?

I have an array with different ID's. 我有一个具有不同ID的数组。 This array is populated from query, so number of ID's can be different each time. 此数组是从查询填充的,因此每次的ID数量可以不同。 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. 因此,我想检查我的eventID是否存在于该数组中,如果eventID在数组中具有匹配的ID,我想提醒消息,否则,我要提交表单。 My problem is when eventID does not match any record in array my form does not get submitted. 我的问题是,当eventID与数组中的任何记录都不匹配时,表单不会提交。 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语句中,我只需要检查数组中的记录之一是否与eventID匹配,是否不需要检查数组中的其他ID。 If anyone can help with this problem please let me know. 如果有人可以解决这个问题,请告诉我。

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. 现在编写代码的方式,将为每个不匹配的元素提交一次表单,因为将对数组中的每个项目检查条件if-else。

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: 或者,你可以return你找到重复尽快打出来的功能:

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!');
}

There are a couple things that could be preventing you. 有几件事可能会阻止您。

  1. Make sure to confirm your event id. 确保确认您的事件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. 您有一个字符串'#eventID#',该字符串在javascript中会很好,它将仍然将'55'评估为55,因此您可以,但是请确保您查看该数字以查看它是否正确。

  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. 换句话说,60不在您的数组中,因此当它在for循环中查看第一个值55时,它将找不到它,因此它将提交,然后在不匹配56时将再次提交。 You should put a break; 你应该休息一下; in the submission code to stop the for loop from continuing if you reach submit. 在提交代码中,如果到达提交,则阻止for循环继续。 These multiple submits might be causing issues. 这些多次提交可能会引起问题。 Even better use index of to look it up. 甚至最好使用of的索引来查找它。 indexOf will return -1 if it is not found so 如果找不到,indexOf将返回-1

    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. 您应该确认提交工作正常,您可能会发现console.log正在执行,这意味着javascript到达了正确的区域,但是提交似乎不起作用,在这种情况下,您知道javascript很好,但是您的提交存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM