如何使用JavaScript进行多维数组交集?
我们需要编写一个JavaScript函数,该函数接受文字值数组的多维数组。我们的函数应该返回输入数组中所有子数组的相交数组。
示例
为此的代码将是-
const arr = [
["garden","canons","philips","universal"],
["universal","ola","uber","bangalore"]
];
const findMultiIntersection = (arr = []) => {
const res = [];
arr.forEach(el => {
const thisObj = this;
el.forEach(element => {
if(!thisObj[element]){
thisObj[element] = true;
}
else{
res.push(element)
};
});
}, {});
return res;
};
console.log(findMultiIntersection(arr));输出结果
控制台中的输出将是-
[ 'universal' ]
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志