1. 程式人生 > >多個數組求交集 js

多個數組求交集 js

如下,求profession對應的物件的陣列的交集

var profession = ["cat", "dog","pig","duck"];
var a = {
  "cat": [1, 2, 3, "12", 4, 22, 11],
  "pig": [2, 3, "12"],
  "dog": [2, 4, 3, "12", 11],
  "duck":[2,11]
};
var jiaoji = a.cat;
var re = profession.reduce(function(pre, cur, index, array) {
  jiaoji = jiaoji.filter(function
(n) {
return a[cur].indexOf(n) != -1; }); return jiaoji; },a.cat); console.log(jiaoji);//2