I am currently dealing with an array of room objects and my task involves removing duplicate objects based on their room_rate_type_id
property:
const rooms = [{
room_rate_type_id: 202,
price: 200
},
{
room_rate_type_id: 202,
price: 200
},
{
room_rate_type_id: 202,
price: 189
},
{
room_rate_type_id: 190,
price: 200
}
];
const newRooms = rooms.filter((room, index, array) => {
const roomRateTypeIds = rooms.map(room => room.room_rate_type_id);
// Returns the first index found.
return roomRateTypeIds.indexOf(room.room_rate_type_id) === index;
});
console.log(newRooms);
In addition to eliminating duplicates based on the room_rate_type_id
, I now have a requirement to include price comparison as well when removing objects.
While I grasp the concept of using the filter method in this scenario, I am seeking guidance on how to efficiently incorporate a price check along with room_rate_type_id
match, preferably utilizing ES6 syntax.