[英]Comparing two arrays in Javascript
I've got two arrays in Javascript which currently look like this, but are updated by HTTP requests (node): 我在Javascript中有两个目前看起来像这样的数组,但是由HTTP请求(节点)更新:
var x = [[292,"2349","902103","9"],[3289,"93829","092","920238"]]
var y = [[292,"2349","902103","9"],[322,"93829","092","920238"],[924,"9320","8932","4329"]]
I'm looking to compare these arrays, so that, if there is an array inside y that is not in x, it will be saved to a new array - z
. 我想比较这些数组,这样,如果y内部的数组不在x中,它将被保存到一个新数组 -
z
。 Note that sometimes the order of arrays inside the arrays will change, but I would not like this to affect the result. 请注意,有时数组内部数组的顺序会发生变化,但我不希望这会影响结果。
If there is an array inside x that is not in y, however, is should not be saved to z
. 但是,如果x内部的数组不在y中,则不应保存到
z
。
I read JavaScript array difference and have been able to replicate this, but if the x
array is not shown in y
, it is printed to z
. 我读了JavaScript数组差异并且能够复制它,但是如果
x
数组没有显示在y
,它将被打印到z
。 I am wondering if it is possible for this not to be stored, only the different items in y
? 我想知道是否有可能不存储,只有
y
的不同项目?
Use a higher-order function that accepts an array (which changes with each iteration of y
) and returns a new function that operates on each element (nested array) in some
. 使用接受数组的高阶函数(随
y
每次迭代而变化)并返回一个新函数,该函数对some
元素(嵌套数组)进行操作。 It returns true
if the arrays contain the same elements regardless of order. 如果数组包含相同的元素而不管顺序,则返回
true
。
function matches(outer) {
return function (el) {
if (outer.length !== el.length) return false;
return el.every(function (x) {
return outer.indexOf(x) > -1;
});
}
}
Iterate over y
and return a list of arrays that aren't in x
. 迭代
y
并返回不在x
中的数组列表。
function finder(x, y) {
return y.filter(function (el) {
return !x.some(matches(el));
});
}
finder(x, y);
You can use this function arrayDiff
. 你可以使用这个函数
arrayDiff
。
It takes two arrays (A and B) and returns an array of all elements that are in the first array and not in the second (A \\ B), with any duplicates removed. 它需要两个数组(A和B),并返回第一个数组中所有元素的数组,而不是第二个数组(A \\ B),并删除任何重复项。 Two array elements are equal if their JSON serialization is the same.
如果它们的JSON序列化相同,则两个数组元素相等。
var x = [[292,"2349","902103","9"],[3289,"93829","092","920238"]];
var y = [[292,"2349","902103","9"],[322,"93829","092","920238"],[924,"9320","8932","4329"]];
var z = arrayDiff(y, x);
// z is [[322,"93829","092","920238"],[924,"9320","8932","4329"]]
// arrayDiff :: [a], [a] -> [a]
function arrayDiff(a1, a2) {
let a1Set = toStringSet(a1),
a2Set = toStringSet(a2);
return Array.from(a1Set)
.filter(jsonStr => !a2Set.has(jsonStr))
.map(JSON.parse);
// toStringSet :: [a] -> Set<String>
function toStringSet(arr) {
return new Set(arr.map(JSON.stringify));
}
}
This should work even if the order in the inner arrays is different. 即使内部数组中的顺序不同,这也应该有效。
I'm assuming you will have only numbers and strings in there and you don't expect a strict comparison between them. 我假设你只有数字和字符串,你不希望它们之间有严格的比较。
var x = [[292,"2349","902103","9"],[3289,"93829","092","920238"]];
var y = [[292,"2349","902103","9"],[322,"93829","092","920238"],[924,"9320","8932","4329"]];
// this will do y \ x
var z = arrDiff(y, x);
console.log(z);
function arrDiff(arr1, arr2) {
var rez = [];
for (var i = 0; i < arr1.length; i++) {
if ( ! contains(arr2, arr1[i])) {
rez.push(arr1[i]);
}
}
return rez;
}
function contains(arr, x) {
x = x.slice().sort().toString();
for (var i = 0; i < arr.length; i++) {
// compare current item with the one we are searching for
if (x === arr[i].slice().sort().toString()) {
return true;
}
}
return false;
}
Try this: 尝试这个:
function getArraysDiff(arr1, arr2) {
var x = arr1.map(function(a) { return a.join("") });
var y = arr2.map(function(a) { return a.join("") });
var z = [];
for ( var i = 0, l = arr1.length; i < l; i++ ) {
if ( y.indexOf(x[i]) == -1 ) {
z.push(arr1[i])
}
}
return z;
}
Or this: 或这个:
x.filter((function(y) {
return function(x) {
return y.indexOf(x.join("")) > -1;
}
}( y.map(function(y) { return y.join("") }) )))
You can use Array.prototype.forEach()
, Array.prototype.every()
, Array.prototype.map()
, Array.prototype.indexOf()
, JSON.stringify()
, JSON.parse()
您可以使用
Array.prototype.forEach()
, Array.prototype.every()
, Array.prototype.map()
, Array.prototype.indexOf()
, JSON.stringify()
, JSON.parse()
var z = [];
y.forEach(function(val, key) {
var curr = JSON.stringify(val);
var match = x.every(function(v, k) {
return JSON.stringify(v) !== curr
});
if (match && z.indexOf(curr) == -1) z.push(curr)
});
z = z.map(JSON.parse);
var x = [ [292, "2349", "902103", "9"], [3289, "93829", "092", "920238"] ]; var y = [ [292, "2349", "902103", "9"], [322, "93829", "092", "920238"], [924, "9320", "8932", "4329"] ]; var z = []; y.forEach(function(val, key) { var curr = JSON.stringify(val); var match = x.every(function(v, k) { return JSON.stringify(v) !== curr }); if (match && z.indexOf(curr) == -1) z.push(curr) }); z = z.map(JSON.parse); console.log(z); document.querySelector("pre").textContent = JSON.stringify(z, null, 2)
<pre></pre>
You have got 2 arrays: 你有2个数组:
var x = [[292,"2349","902103","9"],[3289,"93829","092","920238"]];
var y = [[292,"2349","902103","9"],[322,"93829","092","920238"],[924,"9320","8932","4329"]];
To create the Z array, you need the following function: 要创建Z阵列,您需要以下功能:
function createZ(){
var i,j,k=0,z=[],p=x;
for(j=0;j<y.length;j++){
for(i=0;i<p.length;i++){
if(y[j][0]===p[i][0] && y[j][1]===p[i][1] && y[j][2]===p[i][2] && y[j][3]===p[i][3]){
p.splice(i,1); break;
} else {
z[k++]=y[j]; console.log((y[j][0]===p[i][0])+" "+i+","+j);
}
}
}
return z;
}
Note that the createZ() also prints out the i,j of corresponding entry to the console. 请注意,createZ()还会打印出控制台相应条目的i,j。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.