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

183
Vistas
How to combine my array and json result in one?

I want to combine two json results, but im struggling getting it done..

first one (galleryData):

[
      { "userId": 2, "profile": { "profileImage": "image" } },
      { "userId": 4, "profile": { "profileImage": "image" } },
]

second one (combinations):

{
  data: [
    { round: 1, partner: 2 },
    { round: 2, partner: 4 }
  ]
}

the output im expecting:

{
  data: [
    { round: 1, userId: 2, "profile": { "profileImage": "image" } },
    { round: 2, userId: 4, "profile": { "profileImage": "image" } }
  ]
}

Basically I need the profileImage from one of my result and map it to the correct user id

What I tried so far with no success:

let combinedResult = galleryData["userId"].map((item, i) => Object.assign({}, item, combinations[i]));
almost 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use map and on each callback use find to find the corresponding userId === partner

const galleryData = [
      { "userId": 2, "profile": { "profileImage": "image" } },
      { "userId": 4, "profile": { "profileImage": "image" } },
]

const combinations = {
  data: [
    { round: 1, partner: 2 },
    { round: 2, partner: 4 }
  ]
}

let combinedResult = { 
  data: galleryData.map((item, i) => {
    let combination = combinations.data.find(c => c.partner === item.userId);
    return { ...item, round: combination.round } 
  })
};

console.log(combinedResult)

almost 4 years ago · Santiago Trujillo Denunciar

0

I think a little try using Array.forEach and then merge the Objects

a = [
          { "userId": 2, "profile": { "profileImage": "image" } },
          { "userId": 4, "profile": { "profileImage": "image" } },
    ]
    
        b = {
          data: [
            { round: 1, partner: 2 },
            { round: 2, partner: 4 }
          ]
        }
// inside forEach you can write the logic to get elements from array 'a' as you can use `find` to check which user is needed 
    
        b.data.forEach((i,e) => { b.data[e] = {...i, ...a[e]} })

console.log(b)

almost 4 years ago · Santiago Trujillo Denunciar

0

Hope it will be helpful.

let galleryData = [
      { "userId": 2, "profile": { "profileImage": "image" } },
      { "userId": 4, "profile": { "profileImage": "image" } },
];
let galleryDataAsUserId = {};
galleryData.forEach(elem=>{
    galleryDataAsUserId[elem.userId] = elem;
})
let combinations = {
  data: [
    { round: 1, partner: 2 },
    { round: 2, partner: 4 }
  ]
};
let data = combinations.data;
data.map(elem=>{
    let newElem = elem;
    newElem.profile = galleryDataAsUserId[elem.partner].profile;
});
console.log(data)

almost 4 years ago · Santiago Trujillo 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