Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
How to compare two arrays of Json by each of their elements Id?

I have 2 arrays of objects with the same structure , each one of them came from database query. How can I map the cross section into a new array by comparing the ID value.

tempArray=[{city:london,subject:"testdata", id:7777},   {city:london,subject:"testdata", id:5555}]

tempArray1=[{city:london,subject:"testdata", id:8888},   {city:london,subject:"testdata", id:5555}]
     

I am trying to get the result:

 newArray=[{city:london,subject:"testdata", id:5555}]

This is my try but I failed miserably:

 let newArray=tempArray.map((x)=>{
                if (x.id== tempArray1.forEach((doc)=>{
                
                  return doc.id})) {return x
                  
                }
             
              }) 
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use like below:

const filteredArray = tempArray.filter(obj1 => tempArray1.some(obj2 => obj1.id === obj2.id));
about 4 years ago · Juan Pablo Isaza Denunciar

0

First:

If london is a string value, put it between Quotation marks.

Second:

Instead of using forEach you should use filter and some methods like below:

let newArray = tempArray.filter(x => tempArray1.some(doc => doc.id == x.id));
console.log(newArray)

The some method checks if any array elements pass a test (provided as a function).

The filter method creates a new array filled with elements that pass a test provided by a function.

You can check out the links: filter and some.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda