Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!