Combining two arrays of objects based on common ID& return new customized object

I have two different arrays of objects that share IDs as value. so there is an object in arrayA that share same ID in arrayB. I want to combine each object from array A into the corresponding array B based on the common ID they have. Problem is though I want to pick which key: value pairs from each array to have a final customized array of objects to my needs.
so the return needs to be an object like this
return {
ID:123,
name: arrayA.name,
DOB: arrayB.DOB,
Images: getImages()
}
I found some functions that does merge the two together based on IDS like the below but can’t find a way to customize my object
This works fine but you cannot customize

const mapResultToHotel = (a1, a2) =>
  a1.map(itm => ({
    ...a2.find((item) => (item.code === itm.code) && item),
    ...itm
  }))

This doesn’t work and returns undefined for some reason which I can’t understand

const mapResultToHotel = (arr1, arr2) => {
   arr1.map(dbHotel,) =>

   arr2.map(apiHotel) => {
    if (dbHotel.code === apiHotel.code) {
return {
        code: apiHotel.code,
        name: dbHotel.name,
        categoryCode: apiHotel.categoryCode,
        images: getImages(dbHotel,apiHotel)

      }
    }
  }))
  
}

Thanks in advance

All your parentheses and curly brackets are confusing and sometimes wrong. You will need to fix that first.

.map() expects a callback function. To make your code more readable you could make separate functions:

// declare callback function first, use brackets, curly brackets and return
const dbHotelFunction = (dbHotel) => {
   // body of your function

   return dbHotel
}

// pass the function as callback to the map method
arr1.map(dbHotelFunction)

i see you use different shorthands for the arrow function like here:

a1.map(itm => ({

you omit the brackets around itm, which is possible given you only have a single parameter, but then you have => (, which is very confusing.

or here:

arr1.map(dbHotel,) =>

where your .map() function contains dbHotel, as callback function. the => (arrow function) is after you close map. Then the curly brackets are missing, which is a shorthand for when the body of your function is a single line. The body of your function is more then a single line :wink:

Understanding what you are doing is very important.

You use .map() to loop over your arrays, .map() is a higher order method, which means .map expects a function as argument (which is named a callback function.

undefined means no return value. So very likely you forgot to use return somewhere.

const mapResultToHotel = (dbHotels, apiHotelResults) => {
  return dbHotels.map(dbHotel => apiHotelResults.map(apiHotel => {
    if (dbHotel.code === apiHotel.code) {
      return {

        code: apiHotel.code,
        name: apiHotel.name,
        categoryCode: apiHotel.categoryCode,
        categoryName: apiHotel.categoryName,
        rooms: apiHotel.rooms,
        currency: apiHotel.currencey,
        destinationCode: apiHotel.destinationCode,
        destinationName: apiHotel.destinationName,
        latitude: apiHotel.latitude,
        longitude: apiHotel.longitude,
        maxRate: apiHotel.maxRate,
        minRate: apiHotel.minRate,
        reviews: apiHotel.reviews,
        zoneCode: apiHotel.zoneCode,
        zoneName: apiHotel.zoneName,
        accommodationType: dbHotel.accommodationTypeCode
   

      }
    }
  }))
}```

here it is with no messy brackets and a return. I still get weird undefined results like this (5) [Array(5), Array(5), Array(5), Array(5), Array(5)] it should be array with 5 objects. I get 5 nested with only one object in each nested array with value and the rest is undefined

I can’t test the code at the moment. Can you make a jsbin,jsfiddle,codepen so i can actually have the code with data so i can test. Otherwise, debugging is very difficult

you could also start by inspecting what output you currently get in more detail and compare it to what you expect.

no worries. I managed to do with a combination of forEach loop then destructuring what I want from the array then using spread to push the values to the other array to get what I want. Thank you