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

185
Views
¿Cómo combinar mi matriz y el resultado json en uno?

Quiero combinar dos resultados json, pero me cuesta hacerlo.

primero ( galleryData ):

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

segundo ( combinations ):

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

la salida que estoy esperando:

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

Básicamente, necesito la imagen de profileImage de uno de mis resultados y asignarla a la identificación de usuario correcta

Lo que he intentado hasta ahora sin éxito:

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

0

Puede usar map y en cada devolución de llamada use find para encontrar el ID de usuario correspondiente 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 Report

0

Creo que un poco intente usar Array.forEach y luego combine los Objetos

 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 Report

0

Espero que sea de ayuda.

 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 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!