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

82
Vistas
Create a new array from two different array after matching condition

I have two array, want to create a new array that match array1.id == array2.toyId.

Array1 = [
  {
    id: 2,
    name: 'arujn'
  },
  {
    id: 3,
    name: 'abhinav'
  }
]

Array2 = [
  {
    id: 1,
    toyId: 2,
    label: 'label456',
    type: 'text56'
  },
  {
    id: 2,
    toyId: 2,
    label: 'label22',
    type: 'text88'
  },
  {
    id: 3,
    toyId: 3,
    label: 'labe ffum',
    type: 'text35'
  },
  {
    id: 4,
    toyId: 3,
    label: 'label 8',
    type: 'text66'
  }
]

So, new array have name from array1 and label & type from array2,

That array will look like this:-

I am using javascript for this.

Array3 = [
  {
    name: 'arjun',
    newdata: [
      {
        label: 'label456',
        type: 'text56'
      },
      {
       label: 'label22',
       type: 'text88'
      }
    ]
  },
  {
    name: 'abhinav',
    newdata: [
      {
        label: 'labe ffum',
        type: 'text35'
      },
      {
       label: 'label 8',
       type: 'text66'
      }
    ]
  }
]

Please help me to solve this, Thank you in advance.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

const Array1 = [
  {id: 2,name: 'arujn'},
  {id: 3,name: 'abhinav'}],
 Array2 = [
  {id: 1,toyId: 2,label: 'label456',type: 'text56'},
  {id: 2,toyId: 2,label: 'label22',type: 'text88'},
  {id: 3,toyId: 3,label: 'labe ffum',type: 'text35'},
  {id: 4,toyId: 3,label: 'label 8',type: 'text66'}];

const Array3 = Array1.map(element => ({name: element.name, newdata: Array2.filter(obj => obj.toyId === element.id).map(({label, type}) => ({label, type}) )}))

console.log(Array3);

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