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

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

问题描述

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

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/arrays.计算所有数组共有的值的最佳方法是什么?

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]

我最后用简单的虚拟数据对此进行了测试,它似乎有效.但出于某种原因,我生成的一些包含简单字符串的数组也会自动包含一个附加值,equals: function":

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

使用 filter

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

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