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

170
Visualizações
How to compare t re in the first array? JS

I have two arays. example of arrays:

[{id: 1, name: "test 1" , title : "test 11"} ,
{id: 2, name: "test 2" , title : "test 22"} ,
{id: 3, name: "test 3" , title : "test 3"} ,
{id: 4, name: "test 4" , title : "test 4}]

the difference between these two arrays is only the object located in the second array with id 4

this:

 {id: 4, name: "test 4" , title : "test 4}

What I need ?

I need to loop thought both array or whatever and remove all items which are in the first row.

After loop my neew array need to look like :

newArr = [{id: 4, name: "test 4" , title : "test 4}];

I will not write to you what I was trying to do because I will only confuse you.

EDIT: my try ->

         let concatArray = [...firstArray, ...secondArray];
         let uniqueItems = [...new Set(concatArray)];
         uniqueItems.filter((arr) => firstArray.id !== col.id);

This is no work...

merging two arrays I get nothing. I need to delete all items from the first array just.

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

0

We can use the Array filter function, and the findIndex function which allows us to find the index of an item in the array that meets a certain condition. If the item is not present, the index will be -1, and that's how we can identify the missing objects.

const filtered = secondArr.filter((secondItem) => {
    return firstArr.findIndex(({ id: firstItemId }) => firstItemId === secondItem.id) === -1;
}) 

console.log(filtered); // [ { id: 4, name: 'test 4', title: 'test 4' } ]
about 4 years ago · Juan Pablo Isaza Relatório

0

Lodash if you don't mind.

const firstArr =[{id: 1, name: 'test 1' , title : 'test 11'},{id: 2, name: 'test 2' , title : 'test 22'},{id: 3, name: 'test 3' , title : 'test 3'}];
const secondArr = [{id: 1, name: 'test 1', title : 'test 11'} ,{id: 2, name: 'test 2' , title : 'test 22'} ,{id: 3, name: 'test 3' , title : 'test 3'} ,{id: 4, name: 'test 4' , title : 'test 4'}];

const notInBoth = [
    ..._.differenceWith(firstArr, secondArr, _.isEqual),
    ..._.differenceWith(secondArr, firstArr, _.isEqual),
  ];
  
console.log(notInBoth);
.as-console-wrapper{min-height: 100%!important; top: 0}
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use some:

const firstArr =
  [{id: 1, name: 'test 1' , title : 'test 11'},
  {id: 2, name: 'test 2' , title : 'test 22'},
  {id: 3, name: 'test 3' , title : 'test 3'}];

const secondArr = [{id: 1, name: 'test 1', title : 'test 11'} ,
  {id: 2, name: 'test 2' , title : 'test 22'} ,
  {id: 3, name: 'test 3' , title : 'test 3'} ,
  {id: 4, name: 'test 4' , title : 'test 4'}];

let newArray = secondArr.filter(item => !firstArr.some(item2 => item2.id === item.id));

In spoken words: In newArray, look into secondArr and get me a list of items where they are not found in anywhere in firstArr.

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