简体   繁体   English

比较2D阵列 - 寻找独特的项目

[英]Comparing 2D-arrays - finding unique items

I have 2 arrays and need to identity the items in array2 that do not occur in array1. 我有2个数组,需要识别array2中没有出现在array1中的项。

Only the 1st, 2nd and 4th elements should be compared when checking for uniqueness, the 3rd element (amount in stock) should be left out of the comparison. 在检查唯一性时,只应比较第1,第2和第4个元素,第3个元素(库存量)应排除在比较之外。

var array1 = [['892','NEW','2','blue'],
     ['675','USED','2','white'],
     ['943','NEW','2','green'],
     ['121','USED','2','yellow']];

var array2 = [['892','NEW','1','blue'],
     ['892','NEW','1','yellow'],
     ['121','USED','1','blue'],
     ['121','NEW','1','blue'],
     ['121','USED','1','yellow']];

Result should be: 结果应该是:

 result = [
     ['892','NEW','1','yellow'],
     ['121','USED','1','blue'],
     ['121','NEW','1','blue']];

I've tried reworking several solutions posted to similar questions but haven't been able to work it out for this specific situation. 我已经尝试重新处理针对类似问题发布的几个解决方案,但是却无法针对这种特定情况进行解决。

You could take a hash table and a function which generates a key out of the given array and filter array2 by checking the hash table. 您可以通过检查哈希表来获取哈希表和从给定数组生成密钥的函数,并过滤array2

 function getKey(array) { return [0, 1, 3] .map(function (i) { return array[i]; }) .join('|'); } var array1 = [['892', 'NEW', '2', 'blue'], ['675', 'USED', '2', 'white'], ['943', 'NEW', '2', 'green'], ['121', 'USED', '2', 'yellow']], array2 = [['892', 'NEW', '1', 'blue'], ['892', 'NEW', '1', 'yellow'], ['121', 'USED', '1', 'blue'], ['121', 'NEW', '1', 'blue'], ['121', 'USED', '1', 'yellow']], hash = Object.create(null), result = []; array1.forEach(function (a) { hash[getKey(a)] = true; }); result = array2.filter(function (a) { return !hash[getKey(a)]; }); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

In js it would be something like (changed to shorter code): 在js中它会像(更改为更短的代码):

array2.filter(function(val){
  return !array1.some(function(item){
    return (val[0] == item[0] && val[1] == item[1] && val[3] == item[3]);
  })
});

The only solution would be to compare each element of an array, with every element of the other array. 唯一的解决方案是将数组的每个元素与另一个数组的每个元素进行比较。 When and if you find something equal, you break out of the loop with a return true. 当你找到相同的东西时,你会退出循环并返回true。 If you don't find anything just return false and add the element to your output array. 如果没有找到任何内容,只返回false并将元素添加到输出数组中。 I hope this helps. 我希望这有帮助。

Try this ... whatever index you want to skip just give it in index[] below code 试试这个...你要跳过的索引只需在代码下面的index []中给它

foreach (var numberA in a) { foreach (var numberB in b) { if (numberA == numberB) { if (a.inex[3]) { }else lblDate.Text += numberA.ToString(); foreach(a中的var numberA){foreach(b中的var numberB){if(numberA == numberB){if(a.inex [3]){} else lblDate.Text + = numberA.ToString(); } } } }}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM