I am struggling to retrieve a subset from an array of objects based on specific conditions. I have an array of objects in the following format:
const messages = [
{
summary: '[x1fg66pwq-qft] Second reminder to submit supporting documents',
date: '1624652200',
type: 1
},
{
summary: '[x1fg66pwq-fgh] Final reminder to submit supporting documents',
date: '1629058600',
type: 4
},
{
summary: '[zy9l89ptb-yuw] Please submit your proof of address',
date: '1631708200',
type: 2
},
{
summary: '[ggk9nygh8-pmn] Your application has been successful.',
date: '1634300200',
type: 1
},
]
There is another array which provides the messages to retrieve based on the message id in square brackets of the summary:
const messageIds = ['x1fg66pwq', 'zy9l89ptb'];
The result should be retrieval of the latest messages based on what's in the messageIds array. The date field is in epoch.
const result = [
{
summary: '[x1fg66pwq] Final reminder to submit supporting documents',
date: '1629058600',
type: 4
},
{
summary: '[zy9l89ptb] Please submit your proof of address',
date: '1631708200',
type: 2
},
]
To achieve the above, I tried combining a filter and find which did not work for me:
const result = messages.filter((message) =>
messageIds.find(id => message.summary.includes(testEvent))
);
I would expect the above to return the first result in the array which has the summary specified. However, this always returns the full array for me without filtering. Could someone please help me achieve this?