Is there a more efficient way to create a new array with contact objects that have values matching those in selectedContact
?
selectedContact: number[] = [0,2] //value
contacts: Contact[] = [{
firstName:"Dan";
lastName:"Chong";
email:"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e682878885a68b878f8ac885898b">[email protected]</a>";
value:0;
},
{
firstName:"Mark";
lastName:"Wong";
email:"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="157874677e625578747c793b767a78">[email protected]</a>";
value:1;
},
{
firstName:"Layla";
lastName:"Sng";
email:"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e222f37222f0e232f2722602d2123">[email protected]</a>";
value: 2;
}]
Desired output:
newArray = [{
firstName:"Dan";
lastName:"Chong";
email:"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2440454a476449454d480a474b49">[email protected]</a>";
value:0;
},{
firstName:"Layla";
lastName:"Sng";
email:"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7a161b03161b3a171b131654191517">[email protected]</a>";
value:2;
}];
Current approach:
const newArray: Contact[] = [];
this.selectedContact.forEach(index => {
newArray.push(this.contacts.find(c => c.value === index));
});