gpt4 book ai didi

javascript - 从 JavaScript 中的 2 个数组中获取不匹配的值?

转载 作者:行者123 更新时间:2023-11-29 16:20:38 25 4
gpt4 key购买 nike

假设我们有 2 个数组:

 A [] => 1 2 3 4 5

B [] => 1 2 7 4 5

在这种情况下,jQuery 中是否有任何方法可以给出 2 个数组的不匹配值:

 Result [] => 3 7

最佳答案

hiya 在这里工作演示: http://jsfiddle.net/mbKfT/

好读 http://api.jquery.com/jQuery.inArray/

这使用 inArray 检查元素是否存在,如果没有将它添加到 intersect 数组。

rest demo 会提出任何疑问 :)

代码

var a1 = [1,2,3,4,5];
var a2 = [1,2,7,4,5];
var intersect = [];

$.each(a1, function(i, a1val) {

if ($.inArray(a1val, a2) === -1) {
intersect.push(a1val);
}
});

$.each(a2, function(i, a1val) {

if ($.inArray(a1val, a1) === -1) {
intersect.push(a1val);
}
});
$("div").text(intersect);
alert(intersect + " -- " + matches);


关于javascript - 从 JavaScript 中的 2 个数组中获取不匹配的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10765610/

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