Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查字符串数组中重复项的更好代码_Javascript_Jquery - Fatal编程技术网

Javascript 检查字符串数组中重复项的更好代码

Javascript 检查字符串数组中重复项的更好代码,javascript,jquery,Javascript,Jquery,我有以下代码来检查数组中是否有重复项。代码运行良好。但它使用了一个名为newUniqueArray的新数组。在不使用新数组的情况下,是否有更好的代码用于此目的?这段代码有可能进行优化吗 注意:我使用了jQuery中的inArray和关键字 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript" src="

我有以下代码来检查数组中是否有重复项。代码运行良好。但它使用了一个名为newUniqueArray的新数组。在不使用新数组的情况下,是否有更好的代码用于此目的?这段代码有可能进行优化吗

注意:我使用了jQuery中的
inArray
关键字

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#btnSave').click(function (e) {
            var reportRecipients = "A, a , b,";
            reportRecipients = reportRecipients.toLowerCase();
            checkDuplicate(reportRecipients);
        });

        function checkDuplicate(reportRecipients) {
            if (reportRecipients.length > 1) {
                var recipientsArray = reportRecipients.split(',');
                var newUniqueArray = [];

                for (a in recipientsArray) {
                    var email = $.trim(recipientsArray[a]);

                    if ($.inArray(email, newUniqueArray) == -1) {
                        newUniqueArray.push(email);
                    }
                }

                if (newUniqueArray.length < recipientsArray.length) {
                    alert('Duplicate Exists');
                }

                return false;
            }
        }
    });
</script>
</head>
<body>
<input name="txtName" type="text" id="txtName" />
<input type="submit" name="btnSave" value="Save" id="btnSave" />
</body>
</html>

$(文档).ready(函数(){
$('#btnSave')。单击(函数(e){
var reportRecipients=“A,A,b,”;
reportRecipients=reportRecipients.toLowerCase();
检查副本(报告收件人);
});
函数检查重复(reportRecipients){
如果(reportRecipients.length>1){
var recipientsArray=reportRecipients.split(',');
var newUniqueArray=[];
对于(收件人中的a){
var email=$.trim(recipientsArray[a]);
if($.inArray(电子邮件,newUniqueArray)=-1){
newUniqueArray.push(电子邮件);
}
}
if(newUniqueArray.length
我看不出有任何理由为此使用jQuery:

checkDuplicate = function (reportRecipients) {
    if (reportRecipients.length > 1) {
        var recipientsArray = reportRecipients.split(',');
        for (a in recipientsArray) {
            if(reportRecipients.indexOf(a) != reportRecipients.lastIndexOf(a)){
                return true;
            }
        }
    }
    return false;
}

$('#btnSave').click(function (e) {
            var reportRecipients = "A, a , b,";
            reportRecipients = reportRecipients.toLowerCase();
            if(checkDuplicate(reportRecipients)) alert('Duplicate Exists');
        });

我看不出有任何理由为此使用jQuery:

checkDuplicate = function (reportRecipients) {
    if (reportRecipients.length > 1) {
        var recipientsArray = reportRecipients.split(',');
        for (a in recipientsArray) {
            if(reportRecipients.indexOf(a) != reportRecipients.lastIndexOf(a)){
                return true;
            }
        }
    }
    return false;
}

$('#btnSave').click(function (e) {
            var reportRecipients = "A, a , b,";
            reportRecipients = reportRecipients.toLowerCase();
            if(checkDuplicate(reportRecipients)) alert('Duplicate Exists');
        });

如果您只想在字符串数组上进行测试,那么可以使用JavaScript对象的属性进行测试。它使用哈希表查找属性,这比数组迭代更快

例如:

函数检查重复(reportRecipients){
var recipientsArray=reportRecipients.split(','),
textHash={};

对于(var i=0;i,如果您只想在字符串数组上测试,可以使用JavaScript对象的属性进行测试。它使用哈希表查找属性,比数组迭代更快

例如:

函数检查重复(reportRecipients){
var recipientsArray=reportRecipients.split(','),
textHash={};

对于(var i=0;i请阅读如何正确使用JSFIDLE。此外,请检查语法突出显示。您需要将js代码放入js框架中,并选择适当的框架(JQuery)在左窗格中。请参阅,这是您的代码的更新版本,它可以工作。您的数组由什么组成?@badZoke数组由字符串组成请阅读如何正确使用JSIDLE。此外,请检查语法突出显示。您需要将js代码放在js框架中,并选择适当的框架(JQuery)在左窗格中。请参见,这是您的代码的更新版本,它可以工作。您的数组由什么组成?@badZoke数组由StringsHanks组成。使用
textHash
优于
newUniqueArray
@Lijo有什么好处?对象属性查找通常通过哈希表(O(1))实现,而在数组中,您通过迭代(O)进行查询(n) )。请看这个问题:。虽然,一个对象并不完全是一个哈希表,但请看这个博客:。但在大多数情况下,当您知道您正在测试的内容时,它是有效的。谢谢。使用
textHash
newUniqueArray
@Lijo有什么好处,对象属性查找通常是通过哈希表实现的(O(1)),而在数组中,您通过迭代(O(n))进行查询。请参见以下问题:。尽管对象并不完全是哈希表,但请参见此博客:。但在大多数情况下,当您知道要测试的内容时,它是有效的。