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

95
Visualizações
Determine if an array of objects contains any object of another array in JavaScript

Given the following two array objects:

let origArr = [ { value: 2, label: 'Dog' }, 
                { value: 4, label: 'Cat' },
                { value: 16, label: 'Horse' } 
              ]

let newArr = [
  { value: 2, label: 'Dog' },
  { value: 3, label: 'Bird' },
  { value: 0, label: 'Fish' }
]

what would be the best way to return a new array of just the value of the difference between the origArr and the newArr, that is, where origArr element(s) are not in newArr ?

In the above example, I am after a new array called diffArr = [4,16]

Please note: that all value values within both origArr and the newArr are unique

Not sure if there is an ES2016 means?

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

0

You can use filter and some to do the job.

let origArr = [ { value: 2, label: 'Dog' }, 
                { value: 4, label: 'Cat' },
                { value: 16, label: 'Horse' } 
              ]

let newArr = [
  { value: 2, label: 'Dog' },
  { value: 3, label: 'Bird' },
  { value: 0, label: 'Fish' }
]

const res = origArr.filter(x => !newArr.some(y => y.value === x.value));

console.log(res); // assuming you want the entire filtered objects

const justValues = res.map(x => x.value); // if you want just the filtered values

console.log(justValues)

some - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

filter - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

about 4 years ago · Juan Pablo Isaza Relatório

0

You can get all unique value from newArr using Set and array#map and then filter value which doesn't exist in the set and get all values.

const origArr = [ { value: 2, label: 'Dog' }, { value: 4, label: 'Cat' }, { value: 16, label: 'Horse' } ],
      newArr = [ { value: 2, label: 'Dog' }, { value: 3, label: 'Bird' }, { value: 0, label: 'Fish' } ],
      unique = new Set(newArr.map(o => o.value)),
      difference = origArr
            .filter(({value}) => !unique.has(value))
            .map(o => o.value);
console.log(difference);

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