简体   繁体   English

Javascript:跨多个数组查找共享值

[英]Javascript: Finding shared values across multiple array

What this should be able to do is take in a 2D array filled with one letter values and return an array of all the shared values. 这应该能够做的是将一个充满一个字母值的2D数组放入并返回所有共享值的数组。 This is what I have so far: 这是我到目前为止的内容:

var res = array[0].filter(function(x){
        return array.every(function(y){
             return y.indexOf(x) >= 0
        })
    });
return res;  

This is in some form of working state but only under certain condition which makes it very hit and miss. 这处于某种形式的工作状态,但只能在一定条件下进行,这很容易发生。 Working as intended: 按预期工作:

var array = [["x","x"],
             ["x","x","x"]];

This returns the expected array of ["x","x"] but when like this: 这将返回预期的[“ x”,“ x”]数组,但如下所示:

var array = [["x","x","x"],
             ["x","x"]];

It returns ["x","x","x"] 返回[“ x”,“ x”,“ x”]

As you can see the two arrays only share 2 common x's but the code doesn't reflect that in different situations. 如您所见,这两个数组仅共享2个共同的x,但是代码在不同情况下并不能反映这一点。 Also it should be able to handle arrays with other letters like so: 它还应该能够处理带有其他字母的数组,如下所示:

var array = [["x","x","z","y","y"],
             ["x,"x","x","y"],
             ["x","x","z","y"]];

With something like this it should return ["x","x","y"] as all arrays share 2 common x's and 1 common y 像这样,它应该返回[“ x”,“ x”,“ y”],因为所有数组共享2个公共x和1个公共y

Use a combination of .every and .filter , use .indexOf to check whether a element exists in an array or not. 使用.every.filter的组合,使用.indexOf来检查数组中是否存在元素。

 var array = [ ["x", "x", "z", "y", "y"], ["x", "x", "x", "y"], ["x", "x", "z", "y"] ]; var res = array[0].filter(function(x) { return array.every(function(y) { if (y.indexOf(x) != -1) { y[y.indexOf(x)] = Infinity; return true; } return false; }) }) alert(res) 

Here's another way, using Array methods only availble from IE9 etc. 这是另一种方法,使用仅IE9等可用的Array方法。

 function compareValues() { var arrs = [].slice.call(arguments).sort(function(a,b) { return a.length > b.length; // always iterate shortest array }); return arrs.shift().filter(function(x, i) { // filter the first array return arrs.every(function(arr) { // if all other arrays return arr[i] === x; // have the same value at the same index }) }); } var result = compareValues(["x","x","x","y"], ["x","x","z","y"], ["x","x","z","y","y"]); alert(result); 

This is a proposal with Array.prototype.reduce() , Array.prototype.filter() and Array.prototype.indexOf() for nondestructive search. 这是一个建议,使用Array.prototype.reduce()Array.prototype.filter()Array.prototype.indexOf()进行无损搜索。

 var array = [ ["x", "x", "z", "y", "y"], ["x", "x", "x", "y"], ["x", "x", "z", "y"] ], result = array.reduce(function (r, a) { var last = {}; return r.filter(function (b) { var p = a.indexOf(b, last[b] || 0); if (~p) { last[b] = p + 1; return true; } }); }); document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>'); 

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

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