Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  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]));

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading