How to calculate intersection of multiple arrays in JavaScript? And what does [equals: function] mean?

前端 未结 12 2132
广开言路
广开言路 2020-11-29 06:13

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.

I ha

12条回答
  •  既然无缘
    2020-11-29 06:26

    const intersect = (arrayA, arrayB) => {
        return arrayA.filter(elem => arrayB.includes(elem));
    };
    const intersectAll = (...arrays) => {
        if (!Array.isArray(arrays) || arrays.length === 0) return [];
        if (arrays.length === 1) return arrays[0];
        return intersectAll(intersect(arrays[0], arrays[1]), ...arrays.slice(2));
    };
    

提交回复
热议问题