js怎么将一个二维数组中相同元素提取出来？

11 个回答

a.reduce((pre, cur) => {

``````    let newArr = [];
cur.forEach((v, i) => {
if (pre.indexOf(v) >= 0) {
newArr.push(v);
}
})
pre = newArr;
return pre;
})``````

``    a[0].filter(item => a.every(arr =>arr.includes(item)))``
• 1.7k

``````var a=[[1,2,3,5], [2,3,4], [2,3], [2,3,6,7]];

function getRepeatValues(arr) {
var obj = arr.reduce((res,item) => {
new Set(item).forEach(v => {
if(res[v]) res[v]++;
else res[v] = 1
})
return res;
}, {});
return Object.keys(obj).filter(k => obj[k]==arr.length).map(Number);
}

getRepeatValues(a)``````
``````var a=[[1,2,3,5], [2,3,4], [2,3], [2,3,6,7]];

function getRepeatValues(arr){
const original = arr.flat()
const uniqueItems = new Set();
const duplicates = new Set();
for (const value of original){
if (uniqueItems.has(value)) {
duplicates.add(value);
uniqueItems.delete(value);
} else {
uniqueItems.add(value);
}
}
return [...duplicates]
}
getRepeatValues(a)``````
• 1.6k

1. 首先把这个数组`a`每个成员都去重;
2. 再把去重后的数组`a`降维成1维数组;
3. 统计这个1维数组里每个元素出现的次数,出现次数等于`a`的length的元素就是结果
``````var a = [[1, 2, 3, 5], [2, 3, 4, 4, 4, 4], [2, 3], [2, 3, 6, 7]];

const uniq = arr => [...new Set(arr)];

const flat = arr => arr.reduce((p, i) => [...p, ...i], []);

const count = arr => arr.reduce((p, i) => ({...p, [i]: p[i] ? p[i] + 1 : 1}), {});

const filter = (obj, length) => Object.keys(obj).filter(k => obj[k] === length);

console.log(filter(count(flat(a.map(uniq))), a.length));``````
• 3
• 新人请关照

• 580
``````function getTheSame(arr) {
return arr.reduce(function(a, b) {
return a.filter(function(item) {
return b.includes(item);
});
});
}``````
``````const similar = function(arrs) {
var arr = arrs.shift();
for(var i=arrs.length;i--;){
var p = {"boolean":{}, "number":{}, "string":{}}, obj = [];
arr = arr.concat(arrs[i]).filter(function (x) {
var t = typeof x;
return !((t in p) ? !p[t][x] && (p[t][x] = 1) : obj.indexOf(x) < 0 && obj.push(x));
});
if(!arr.length) return null;
}
return arr;
}``````

``````var a = [[1, 2, 3, 5], [2, 3, 4], [2, 3], [2, 3, 6, 7]];

// 1.降维
var arr1 = [].concat(...a)
console.log(arr1)

// 2.去重
var uniq = [...new Set(arr1)]
console.log(uniq)

// 3. 计数
var counter = arr1.reduce((acc, v) => {
if (v in acc) {
acc[v]++
} else {
acc[v] = 1
}
// 4.非重
// if(acc[v] > 1 && uniq.indexOf(v) > -1){
//     uniq.splice(uniq.indexOf(v),1)
// }
return acc
}, {})
console.log(counter)

// 4.求重
var duplicate = uniq.filter(e => counter[e] > 1)
console.log(duplicate)
``````

const list = [[1,2,3,5], [2,3,4], [2,3], [2,3,6,7]];
const rlt = list.reduce((sameV, item) => {

``````const newSame = [];
if (sameV && sameV.length > 0) {
item.filter((inner) => {
if (sameV.indexOf(inner) > -1) {
newSame.push(inner);
}
});
}
return newSame;``````

});
console.log(rlt);

• 439

``````var a = [
[1, 2, 3, 5],
[2, 3, 4],
[2, 3],
[2, 3, 6, 7]
]
let obj = {}
let repeatAry = []

function mapAry(ary) {
ary.forEach(item => {
if (Array.isArray(item)) {
mapAry(item)
} else {
obj[item] = (obj[item] || 0) + 1
obj[item] === 2 && repeatAry.push(item)
}
})
}
mapAry(a)
console.log(repeatAry)``````