In MySQL if I have two tables like this:
appointments
appointment_id patient_id appointment_time
-------------------------------------------------------
1 42 2017-10-17 08:00:00
2 43 2017-10-17 08:30:00
and
patients
patient_id name
---------------------------
42 Joe Smith
43 Sally Sue
44 Jim Bob
then I could left join the two tables like this
SELECT *
FROM appointments
LEFT JOIN patients ON appointments.patient_id = patients.patient_id
and I would get a result like this:
appointment_id patient_id appointment_time patient_id name
----------------------------------------------------------------------------------
1 42 2017-10-17 08:00:00 42 Joe Smith
2 43 2017-10-17 08:30:00 43 Sally Sue
So my question is, in Javascript (ES6+), if I have two arrays of objects like this:
const appointments = [
{
appointmentId: 1,
patientId: 42,
appointmentTime: '2017-10-17 08:00:00'
},
{
appointmentId: 2,
patientId: 43,
appointmentTime: '2017-10-17 08:30:00'
}
];
and
const patients = [
{
patientId: 42,
name: 'Joe Smith'
},
{
patientId: 43,
name: 'Sally Sue'
},
{
patientId: 44,
name: 'Jim Bob'
}
];
How can I left join the patients array onto the appointments array based on the patientId
property?
[
{
appointmentId: 1,
patientId: 42,
appointmentTime: '2017-10-17 08:00:00',
name: 'Joe Smith'
},
{
appointmentId: 2,
patientId: 43,
appointmentTime: '2017-10-17 08:30:00',
name: 'Sally Sue'
}
]
I know that there's the unionBy
method in lodash, but that will remove the properties in the appointments object.