Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
Find common elements from arrays's which are inside an Object - Javascript

So I have an Object that contains multiple arrays

x = {
    c: ['Full_Name', 'Last_Name', 'Email', 'Organizations', 'MISC', 'UNIQUEID']
    small: ['Title', 'Organizations', 'URL', 'UNIQUEID']
    .
    .
    .}

What is the best and the fastest way to find all the similar elements from all these arrays?

So for example in this case the answer will be ['Organization', 'UNIQUEID']

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

a possible approach using reduce and Set. for the accumulator I'm initializing it with the first array (x.c). Doesn't matter which one since in the end we are taking intersection anyway. Inside the reduce I'm intersecting the accumulator with the current iteration array

const x = {
  c: ['Full_Name', 'Last_Name', 'Email', 'Organizations', 'MISC', 'UNIQUEID'],
  small: ['Title', 'Organizations', 'URL', 'UNIQUEID'],
  big: ['abc', 'Organizations', 'def', 'UNIQUEID']
}


const res = Object.values(x).reduce((acc, curr) => {
  return new Set(curr.filter(i => acc.has(i)))
}, new Set(x.c))

console.log([...res])

almost similar with a for loop except I'm skipping first element in loop

const x = {
  c: ['Full_Name', 'Last_Name', 'Email', 'Organizations', 'MISC', 'UNIQUEID'],
  small: ['Title', 'Organizations', 'URL', 'UNIQUEID'],
  big: ['abc', 'Organizations', 'def', 'UNIQUEID']
}

const values = Object.values(x)
let acc = new Set(values[0])
for (let i = 1; i < values.length; i++) {
  acc = new Set(values[i].filter(j => acc.has(j)))
}

console.log([...acc])

about 4 years ago · Juan Pablo Isaza Relatório

0

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" ]
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda