I want to extract a collection of objects from an array based on their ID. I am using vanilla Javascript.
contacts = [
{ID: 1, firstName: "Aaron", lastName: "Smith"},
{ID: 2, firstName: "Ben", lastName: "Smith"},
{ID: 3, firstName: "Conrad", lastName: "Smith"}
];
filteredContactIds = [1, 3];
filteredContacts = [];
filteredContactIds.forEach(function (filteredId) {
filteredContacts.push(
contacts.forEach(function (contact) {
if (contact.ID == filteredId) {
return contact;
}
})
)
});
Contacts and filteredContactIds are simplified, in my code both arrays are both populated correctly. The problem is, the filteredContacts array is only receiving the ID property of each contact object, I want to insert the whole object.
I have exhausted my limited understanding. Can anyone point out the problem?