JavaScript
const data = [
{
reservationNumber: 'RX-0032466',
fullName: 'John Doe',
firstName: 'John',
lastName: 'Doe',
documentType: 'dni',
hotel: 'hpp',
documentID: null,
extra_rooms: [],
extraRoomsAges: [],
mainRoomDistribution: {
adults: 2,
infants: 0,
},
totalCharges: 1754.28,
agencyCode: 5,
roomNumber: '63',
},
{
reservationNumber: 'RX-0032466',
fullName: 'John Doe',
firstName: 'John',
lastName: 'Doe',
documentType: 'dni',
hotel: 'hpp',
documentID: null,
extra_rooms: [],
extraRoomsAges: [],
mainRoomDistribution: {
adults: 3,
infants: 1,
},
totalCharges: 1954.49,
agencyCode: 5,
roomNumber: '64',
},
{
reservationNumber: 'RX-0032466',
fullName: 'John Doe',
firstName: 'John',
lastName: 'Doe',
documentType: 'dni',
hotel: 'hpp',
documentID: null,
extra_rooms: [],
extraRoomsAges: [],
mainRoomDistribution: {
adults: 1,
infants: 0,
},
totalCharges: 850.30,
agencyCode: 5,
roomNumber: '65',
},
{
reservationNumber: 'RX-0032494',
fullName: 'Jane Foster',
firstName: 'Jane',
lastName: 'Foster',
documentType: 'dni',
hotel: 'hpp',
documentID: '123456789',
extra_rooms: [],
extraRoomsAges: [],
mainRoomDistribution: {
adults: 4,
infants: 0,
},
totalCharges: 1540,
agencyCode: 13,
roomNumber: '40',
},
]
function mergeObjectsInUnique(array, property) {
const newArray = new Map();
array.forEach((item) => {
const propertyValue = item[property];
newArray.has(propertyValue)
? newArray.set(propertyValue, { ...item, ...newArray.get(propertyValue) })
: newArray.set(propertyValue, item);
});
return Array.from(newArray.values());
}
console.log(mergeObjectsInUnique(data, 'reservationNumber'))