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

242
Visualizações
atleast one of the value's in the array1 obj "data" array matches the "data" array in array2 object

at least one of the value's in the array1 obj "data" array matches the "data" array in array2 object


> array1

array1 = [
  {
      id: '1',
      name: 'ron',
      data: ['p1']
  },
  {
      id: '2',
      name: 'lon',
      data: ['p2']
  },
{
      id: '3',
      name: 'voon',
      data: ['p4']
  }
];


> array2

array2 = [
  {
      id: '1',
      name: 'fgr',
      data:['p1','p2','p3']
  },
  {
      id: '2',
      name: 'gone',
      data:['p1','p2','p3']
  }
]


output: { id: '1', name: 'ron', data: ['p1'] }, { id: '2', name: 'lon', data: ['p2'] }

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

0

With the below assumption:

  • array1 has objects each of which always have prop data which is an array
  • array2 also has objects and these objects will always have prop data which is an array as well
  • the desired objective is to generate an array with objects from array1 whose data have at least one common element with any of the data of array2 objects.

this solution may work:

const findNeedleInHaystack = (needle = array1, haystack = array2) => (
    needle.reduce((acc, itm) => (
      haystack
      .map(x => x.data)
      .flat()
      .some(x => itm.data.includes(x))
        ? [...acc, itm]
        : [...acc]
    ), [])
);

Explanation

  • iterate over the needle array (ie, array1) using .reduce
  • use .map on haystack array (ie, array2) to separate the data from each object
  • use .flat() to transform the 2-dimensional resulting array into 1-dimension
  • use .some to see if any element in the 1-dimensional array is also present in itm's data array
  • if found, concatenate itm to the result (using ... spread operator on acc - the aggregator/accumulator)
  • if not found, skip concatenating itm.

Code Snippet

const array1 = [
  {
      id: '1',
      name: 'ron',
      data: ['p1']
  },
  {
      id: '2',
      name: 'lon',
      data: ['p2']
  },
{
      id: '3',
      name: 'voon',
      data: ['p4']
  }
];

const array2 = [
  {
      id: '1',
      name: 'fgr',
      data:['p1','p2','p3']
  },
  {
      id: '2',
      name: 'gone',
      data:['p1','p2','p3']
  }
];

const findNeedleInHaystack = (needle = array1, haystack = array2) => (
    needle.reduce((acc, itm) => (
    haystack.map(x => x.data).flat().some(x => itm.data.includes(x))
        ? [...acc, itm]
      : [...acc]
  ), [])
);

console.log(findNeedleInHaystack());

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