gpt4 book ai didi

Jquery 检查数组是否包含重复字符串

转载 作者:行者123 更新时间:2023-12-01 00:34:34 25 4
gpt4 key购买 nike

如何检查数组是否包含重复字符串,我使用了 validateArray = ['sa','sa','yu']以下功能来自 SO,但对我来说同样不起作用。

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

最佳答案

这对我有用:

var reportRecipients = ['AAA', 'XYZ', 'AAA', 'ABC', 'XXX', 'XYZ', 'PQR'];
var recipientsArray = reportRecipients.sort();

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

希望有帮助。

关于Jquery 检查数组是否包含重复字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22979762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com