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

201
Visualizações
How can I compare two arrays to create a new one?

I make two api call and I need to compare the results and create a new array that contains only the values of the second call that are not equals to first

  const listaRealmTrovata = async () => {
    await axios.get(APICALL1)
      .then((realmUserAttivi) => {
        axios.get(APICALL2)
          .then((realmAttivi) => {
            realmUserAttivi.data.map((realmUserAttivo) => {
              realmAttivi.data.filter((res) => {
                if (realmUserAttivo.realm.id !== res.id) {
                  setListaRealm(previousListRealm => [...previousListRealm, { value: res.id, label: res.realmId }]);
                }
              })
            })
          })
      })
  };

In this way I obtain a list with with repeated values, because I compare each element of the first array each time with the elements of the second array

Example:

 realmUserAttivi.data : [
   0: {id:1}
   1: {id:2}
   3: {id:3}
]

 realmAttivi.data : [
   0: {id:1}
   1: {id:5}
   3: {id:3}
]

so the result that I would to obtain is:

listaRealm: [0: {id:5}] // the only one that isn't in equal in the two array.

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

0

Here the variable notPresent will contain data from realmAttivi which is not found in realmUserAttivo.

//api call code
.then((realmAttivi) => {
    const notPresent = realmAttivi.filter(ra => !realmUserAttivo.some(rua => rua.id === ra.id));
    setListaRealm(previousListRealm => [...previousListRealm, ...notPresent]);
})
about 4 years ago · Juan Pablo Isaza Relatório

0

 realmUserAttivi = [
   {id:1},
   {id:2},
   {id:3},
]

 realmAttivi = [
   {id:1},
   {id:5},
   {id:3}
]      


const notRepeated = (arr1, arr2) => 
  arr2.filter(objArr2 => !arr1.find(objArr1 => objArr2.id == objArr1.id))

notRepeated(realmUserAttivi,realmAttivi)

this will return [{id:5}]

about 4 years ago · Juan Pablo Isaza Relatório

0

I thought if the array become huge , since the algorithm being used is n^2 ,it might freeze the page, since JS would be busy ,tried making it async .

let realmUserAttivi = [
   {id:1},
   {id:2},
   {id:3},
],
 realmAttivi = [
   {id:1},
   {id:5},
   {id:3}
    ]  
async function findCommon() {
    let promises = []
    for (let i = 0; i < realmAttivi.length; i++)
    {
       promises.push(SetTimoutPromise(i))
    }
    let res = await Promise.all(promises)
    res = res.filter(e => e)
    return res
}


 let SetTimoutPromise =(index)=> new Promise((resolve, reject) => {
             setTimeout(() => {
                if (!realmAttivi.find((objArr1 => realmUserAttivi[index].id == objArr1.id)))
                {
                    resolve(realmAttivi[index])
                }
            resolve()
        },0)
    })
findCommon()
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