简体   繁体   English

比较javascript中常见值的未指定数量的数组

[英]Compare an unspecified number of arrays for common values in javascript

I would like to know how to compare two or more -- potentially unlimited -- arrays for common values and push these values into a new array efficiently. 我想知道如何比较两个或更多 - 可能是无限的 - 数组的常见值,并有效地将这些值推入一个新的数组。 Below I have a function that will accept unlimited arguments, but I am uncertain if this is a good place to begin. 下面我有一个接受无限参数的函数,但我不确定这是否是一个好的开始。 PHP appears to have a method that can do what I want called array_intersect. PHP似乎有一个方法可以做我想要的array_intersect。 Does javascript offer something similar? javascript提供类似的东西吗?

Note: I have found examples of how this can be done with two or so arrays, but I have not found examples of how such approaches might be applied to an unspecified number of arrays as of yet. 注意:我已经找到了如何使用两个左右的数组来完成此示例的示例,但我还没有找到如何将此类方法应用于未指定数量的数组的示例。 Therefore I do not see this as a duplicate question. 因此,我不认为这是一个重复的问题。

To further clarify, the arrays might be filled with anything. 为了进一步澄清,阵列可能充满了任何东西。 Letters, numbers, symbols, words, you name it, it might be there. 字母,数字,符号,单词,你的名字,它可能在那里。

 var sampleOne = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; var sampleTwo = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]; function FindDirectRelation() { for(var i = 0; i < arguments.length; ++i) { console.log(arguments[i]); }; }; var directRelation = FindDirectRelation(sampleOne, sampleTwo); 

I am still a coding novice, so please ensure that everything is explained in a way that is simple enough for me to understand. 我仍然是编码新手,所以请确保以一种简单易懂的方式解释所有内容。

using an existing intersect that works with 2 arrays, we can chain together a common sub-set using the built-in reduce() method on an array of arrays that need intersected: 使用与2个数组一起使用的现有相交,我们可以使用内置的reduce()方法将一个公共子集链接在一个需要交叉的数组数组上:

function intersect(a, b) {
  var aa = {};
  a.forEach(function(v) { aa[v]=1; });
  return b.filter(function(v) { return v in aa; });
}

var r1=[1,2,3], 
r2=[1,3,4,5], 
r3=[5,1,3];

alert([r1, r2, r3].reduce(intersect)) // shows: 1,3

if you define "intersect" as just being in more than one array (not every), then it's more complex... 如果你将“intersect”定义为只是在多个数组中(不是每个数组),那么它就更复杂了......

Check to make sure the elements in the first array are also in the remaining arrays: 检查以确保第一个数组中的元素也在其余数组中:

function multi_intersect(a) {
  var other_arrays = Array.prototype.slice.call(arguments, 1);

  return a . filter(function(elt) {
    return other_arrays.every(function(an) {
      return an.indexOf(elt) !== -1;
    });
  });
}

Try using Array.prototype.filter() , Array.prototype.indexOf() 尝试使用Array.prototype.filter()Array.prototype.indexOf()

var res = sampleOne.filter(function(val) {return sampleTwo.indexOf(val) !== -1})

 var sampleOne = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; var sampleTwo = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]; var arr = ["a", "b", "c"]; var arr1 = ["c", "d", "e"]; var arr2 = [2, 7]; function samples() { var args = Array.prototype.slice.call(arguments); var res = []; for (var i = 0, curr, next; i < args.length; i++) { if (args[i + 1]) { // set `curr` to array `i` curr = args[i]; // set `next` to array `i + 1` if it exists next = args[i + 1] } else { // if at last index, set `curr` to `args` : input arrays // flattened to single array , with element at `i` removed curr = [].concat.apply([], args.slice(0, args.length - 1)); console.log(curr) // set next to current index next = args[i]; }; next = next.filter(function(val) { return curr.indexOf(val) !== -1 // filter duplicate entries at `res` && res.indexOf(val) === -1 }); res = res.concat.apply(res, next); }; return res } var sample = samples(sampleOne, sampleTwo, arr, arr1, arr2); console.log(sample); // [5, 6, 7, 8, 9, 10, 11, 12, "c", 2] 

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

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