如何计算JavaScript中多个数组的交集?什么[等于:功能]是什么意思? [英] How to calculate intersection of multiple arrays in JavaScript? And what does [equals: function] mean?

查看:150
本文介绍了如何计算JavaScript中多个数组的交集?什么[等于:功能]是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题,最简单的数组交集代码但是所有的解决方案都假定数组的数量是两个,在我的情况下这是不确定的。

I am aware of this question, simplest code for array intersection but all the solutions presume the number of arrays is two, which cannot be certain in my case.

我有一个div包含数组的数据的页面。我想找到所有数组共有的值。我不知道我将提前有多少个div /数组。计算所有数组共有的值的最佳方法是什么?

I have divs on a page with data that contains arrays. I want to find the values common to all arrays. I do not know how many divs/arrays I will have in advance. What is the best way to calculate values common to all arrays?

var array1 = ["Lorem", "ipsum", "dolor"];
var array2 = ["Lorem", "ipsum", "quick", "brown", "foo"];
var array3 = ["Jumps", "Over", "Lazy", "Lorem"];
var array4 = [1337, 420, 666, "Lorem"];
//Result should be ["Lorem"];

我在其他地方使用Underscore.js找到了另一种解决方案。

I found another solution elsewhere, using Underscore.js.

var arrayOfArrays = [[4234, 2323, 43], [1323, 43, 1313], [23, 34, 43]];
_.intersection.apply(_, arrayOfArrays)
//Result is [43]

我用简单的虚拟数据对此进行了测试,结果似乎有效。但由于某些原因,我正在制作的一些包含简单字符串的数组也自动包含一个附加值,等于:函数:

I've tested this with simple dummy data at my end and it seems to work. But for some reason, some of the arrays I'm producing, which contain simple strings, also automatically include an added value, "equals: function":

["Dummy1", "Dummy2", "Dummy3", equals: function]

每当我使用Underscore.js交集方法时,在数组数组中,我总是在开发工具中得到[equals:function],而不是 - 如果Dummy3对所有数组都是通用的 - [Dummy3]。

And whenever I use the Underscore.js intersection method, on an array of arrays, I always get [equals: function] in dev tools, and not - if "Dummy3" is common to all arrays - ["Dummy3"].

所以TL; DR是否有另一种适合我的情况的阵列交叉解决方案?任何人都可以解释[equals:function]在这里意味着什么?当我在开发工具中展开项目时,它会生成一个空数组和数组上可用的方法列表(pop,push,shift等),但这些方法都已淡出,而equals:function则突出显示。

So TL;DR is there another solution to array intersection that would suit my case? And can anyone explain what [equals: function] means here? When I expand the item in the dev tools, it produces an empty array and a list of methods available on arrays (pop, push, shift etc), but these methods are all faded out, while equals: function is highlighted.

推荐答案

我为此写了一个辅助函数:

I wrote a helper function for this:

function intersection() {
  var result = [];
  var lists;

  if(arguments.length === 1) {
    lists = arguments[0];
  } else {
    lists = arguments;
  }

  for(var i = 0; i < lists.length; i++) {
    var currentList = lists[i];
    for(var y = 0; y < currentList.length; y++) {
        var currentValue = currentList[y];
      if(result.indexOf(currentValue) === -1) {
        var existsInAll = true;
        for(var x = 0; x < lists.length; x++) {
          if(lists[x].indexOf(currentValue) === -1) {
            existsInAll = false;
            break;
          }
        }
        if(existsInAll) {
          result.push(currentValue);
        }
      }
    }
  }
  return result;
}

像这样使用:

intersection(array1, array2, array3, array4); //["Lorem"]

或者像这样:

intersection([array1, array2, array3, array4]); //["Lorem"]

完整代码此处

更新1

使用 此处稍微小一点的实现过滤器

A slightly smaller implementation here using filter

这篇关于如何计算JavaScript中多个数组的交集?什么[等于:功能]是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆