jquery 如何使用javascript比较两个数组和基于索引的不匹配?[duplicate]

unhi4e5o  于 2022-12-12  发布在  jQuery
关注(0)|答案(2)|浏览(79)

此问题在此处已有答案

How to compare two arrays and then return the index of the difference?(4个答案)
How to get the difference between two arrays in JavaScript?(83个答案)
Javascript compare 2 arrays index by index, and not by total number of values the 2 arrays have in common(2个答案)
4天前关闭。

var a=["a","b","c","d"];
var b=["b","a","c","d"];

我需要的输出如下:
不匹配的数组
不匹配数组= ["a", "b"];

lymnna71

lymnna711#

您可以使用筛选函数并检查两个数组中索引的值,然后从中获取不匹配的值。

var a = ["a", "b", "c", "d"];
var b = ["b", "a", "c", "d"];

var c = a.filter(function (item, index) {
  return item !== b[index];
});

console.log(c);
wgx48brx

wgx48brx2#

这里我使用Javascript的filter方法来获取不匹配的Array。

const a = ["a","b","c","d"];
const b = ["b","a","c","d"];

const mismatchedArr = a.filter((aItem,index) => aItem !== b[index]);
console.log(mismatchedArr); //Prints ["a","b"]

相关问题