简体   繁体   English

比较jQuery中的两个字符串不起作用

[英]Comparing two strings in jQuery doesn't work

This is my code 这是我的代码

function nameIsDuplicate(name){
    objects = $("#content").find('p.itemOldName');
    $(objects).each(function(i, object){
        console.log("*"+($(object).text()).toLowerCase() + "*" + name.toLowerCase()+"*");
        if(($(object).text()).toLowerCase() == name.toLowerCase())
            return true;
    });
    return false;
}

I am building an online file manager system. 我正在构建一个在线文件管理器系统。 the name argument is a name provided by the user via a textbox, and the $(object).text() is the name of files and folders in the current directory. name参数是用户通过文本框提供的名称, $(object).text()是当前目录中文件和文件夹的名称。 These names come via exec("ls") command. 这些名称来自exec("ls")命令。

I need to check if the name provided by the user already exists. 我需要检查用户提供的名称是否已经存在。 So I compare the name with every files/folders name. 因此,我将name与每个文件/文件夹名称进行比较。 The problem is it doesn't find duplicates. 问题是它找不到重复项。 The result of the above code is given in the following image 下图给出了以上代码的结果

在此处输入图片说明

The return true returns out of the each callback. return true返回each回调。 That has no effect on each (it only cares about return false ) and doesn't do anything to set the return value of nameIsDuplicate . 这对each都没有影响(它只关心return false ),并且不做任何事情来设置nameIsDuplicate的返回值。

You want to return false there (no need to keep looking) and set a flag so your nameIsDuplicate can return it: 您要在那里return false (无需继续查找)并设置一个标志,以便您的nameIsDuplicate可以返回它:

function nameIsDuplicate(name){
    var duplicate = false;
    objects = $("#content").find('p.itemOldName');
    $(objects).each(function(i, object){
        console.log("*"+($(object).text()).toLowerCase() + "*" + name.toLowerCase()+"*");
        if(($(object).text()).toLowerCase() == name.toLowerCase()) {
            duplicate = true;
            return false; // Stop looping
        }
    });
    return duplicate;
}

However , that function can be a lot simpler using Array.prototype.some : 但是 ,使用Array.prototype.some可以使该函数简单得多:

function nameIsDuplicate(name){
    var objects = $("#content").find('p.itemOldName');
    name = name.toLowerCase();
    return objects.get().some(function(object) {
        return $(object).text().toLowerCase() === name;
    });
}

some calls its callback for each entry in the array. some调用数组的每个条目的回调。 If the callback returns a falsy value, some keeps going; 如果回调返回错误的值,则some继续进行; if the callback returns a truthy value, some stops. 如果回调返回真实值,则some停止。 some 's return value is true if a call to the callback returned a truthy value, false if not. 如果对回调的调用返回了真实值,则some的返回值是true否则返回false

Your function doesn't return true , because you are in each loop... should be something like this: 您的函数不返回true ,因为你在each循环......应该是这样的:

function nameIsDuplicate(name){
    var same=0;
    objects = $("#content").find('p.itemOldName');
    $(objects).each(function(i, object){
        console.log("*"+($(object).text()).toLowerCase() + "*" + name.toLowerCase()+"*");
        if(($(object).text()).toLowerCase() == name.toLowerCase()){
            same=1; 
            return false;        
        }
    });
    if(same){
        return true;
    }else{
        return false;
    }
}

We can break the $.each() loop at a particular iteration by making the callback function return false. 通过使回调函数返回false,可以在特定的迭代中破坏$ .each()循环。 Returning non-false is the same as a continue statement in a for loop; 返回非false与for循环中的continue语句相同; it will skip immediately to the next iteration. 它将立即跳至下一个迭代。

Other solution: 其他解决方案:

function nameIsDuplicate(name){
    return $("#content").find('p.itemOldName').filter(function(){return $(this).text().toLowerCase() === name.toLowerCase();}).length;
}

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

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