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

前端 未结 12 2146
广开言路
广开言路 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:44

    Lodash pure:

    _.keys(_.pickBy(_.groupBy(_.flatten(arrays)), function (e) {return e.length > 1}))
    

    Lodash with plain js:

    var elements = {}, duplicates = {};
     _.each(arrays, function (array) {
         _.each(array, function (element) {
             if (!elements[element]) {
                 elements[element] = true;
             } else {
                 duplicates[element] = true;
             }
         });
     });
    _.keys(duplicates);
    

提交回复
热议问题