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

193
Visualizações
How can i compare three array of objects so i an add or remove new values?

I have three array of objects. The first array of objects is the original state.

let originalArrState = [
    {
        id:1,
        name:'Name 1 from id 1',
        number: 15 // => this needs to be removed based on the value `number`
        // in secondArrCompare - because we have there object with id of 1
    },
    {
        id:1,
        name:'Name 2 from id 1',
        number: 125
    },
    {
        id:1,
        name:'Name 3 from id 1',
        number: 130
    },

    // 135 needs to be added based on the value `number` in firstArrCompare because we have there 
    // object with id of 1
]

now i have other two array of objects which i need to compare them with the original arr

let firstArrCompare = [
    {
        id:1,
        number:135
    }
]

let secondArrCompare = [
    {
        id:1,
        number:15
    }
]

the idea is that it needs to be dynamically - so based on the values in secondArrCompare we need to remove objects from originalArrState and based on the firstArrCompare we need to add new one after we remove the previous.

So the result should be at the end array of objects with values 125 130 135.

let originalArrState = [
    {
        id:1,
        name:'Name 2 from id 1',
        number: 125
    },
    {
        id:1,
        name:'Name 3 from id 1',
        number: 130
    },
    {
        id:1,
        number: 135
    },
]

If firstArrCompare and secondArrCompare are empty than nothing should be added or removed. The values inside will be always one to one - so one value with same id in firstArrCompare and one in secondArrCompare.

Also there could be multiply objects with anotber id - for example id-2 which will mutate the original state.

For example

let firstArrCompare = [
    {
        id:1,
        number:135
    },
    {
        id:2,
        number:1000
    }
    // we search for id 2 in original arr so we can add this after the removing
]

let secondArrCompare = [
    {
        id:1,
        number:15
    },
    {
        id:2,
        number:100
    }
    // now we need to search for id 2 in original arr to remove that
]

So if there is id 1 in first arr, there will be always object with id 1 in the second array - so one to one relationship.

i got stuck at

 var newArr = originalArrState.filter(itemOne => {
        return !secondArrCompare.some(itemTwo => {
          return itemOne.id == itemTwo.id;
        })
      })
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Doing some changes to your implementation. Had to filter both id and number combination matches otherwise all elements with id:1 will be removed. We get newArr from this.
After that looped over the firstArrCompare and add to the newArray if element with same id is present in newArr

let originalArrState = [{
        id: 1,
        name: 'Name 1 from id 1',
        number: 15
    },
    {
        id: 1,
        name: 'Name 2 from id 1',
        number: 125
    },
    {
        id: 1,
        name: 'Name 3 from id 1',
        number: 130
    },
]

let firstArrCompare = [{
        id: 1,
        number: 135
    },
    {
        id: 2,
        number: 1000
    }
]

let secondArrCompare = [{
        id: 1,
        number: 15
    },
    {
        id: 2,
        number: 100
    }
]

var newArr = originalArrState.filter(itemOne => {
    return !secondArrCompare.some(itemTwo => {
        return itemOne.id === itemTwo.id && itemOne.number === itemTwo.number;
    })
})
firstArrCompare.forEach((el) => {
    if (newArr.some((item) => item.id===el.id)){
    newArr.push(el)
  }
})

console.log(newArr)

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