You're looking for a Set intersection -
function intersection2(a, b) {
if (a.size > b.size)
return intersection2(b, a)
const r = new Set
for (const v of a)
if (b.has(v))
r.add(v)
return r
}
You can compute the intersection of many sets by combining reduce and intersection2 -
function intersection(s, ...sets) {
return sets.reduce(intersection2, s)
}
To use it with your code, get all values of x using Object.values and map over them to create an array of sets. Finally call intersection with all of the sets -
const x = {
c: ['Full_Name', 'Last_Name', 'Email', 'Organizations', 'MISC', 'UNIQUEID'],
small: ['Title', 'Organizations', 'URL', 'UNIQUEID'],
big: ['abc', 'Organizations', 'def', 'UNIQUEID']
}
const result =
intersection(...Object.values(x).map(v => new Set(v)))
console.log(result)
Set [ "Organizations", "UNIQUEID" ]
If you want the result as an array, you can convert it using Array.from -
console.log(Array.from(result))
[ "Organizations", "UNIQUEID" ]
xis[]? Should that always produce an empty array as the result? Or should the result array still contain some values since, for example, "UNIQUEID" is in more than one (but not all) arrays inx?