I'm looking for an efficient way of returning a duplicate value where the duplicate exists in each array within an array.
const array = [
['qwerty'],
['abc', 'qwerty'],
['foo', 'qwerty']
]
Qwerty would be returned since it exists in all 3 nested arrays. If it only existed in two, nothing would be returned.
I was thinking something along the lines of
const arr = [];
array.map(item =>
item.map((nestedItem) => {
arr.push(nestedItem);
const findDuplicates = (data) =>
data.filter(
(item, index) =>
data.indexOf(item) !== index,
);
if (
findDuplicates(arr).length ===
array.length
) {
return findDuplicates(arr); // This logic here does not work as the count is 1 vs 3
}
}),
);
Note there could be more than 3 nest arrays or less.
Definitely this is not the right way, I am still new and not good. TIA