I want to create a new array containing contact objects if the value property in contacts matches the values in selectedContact. Any simpler way to do this?
selectedContact: number[] = [0,2] //value
contacts: Contact[] = [{
firstName:"Dan";
lastName:"Chong";
email:"[email protected]";
value:0;
},
{
firstName:"Mark";
lastName:"Wong";
email:"[email protected]";
value:1;
},
{
firstName:"Layla";
lastName:"Sng";
email:"[email protected]";
value: 2;
}]
Intended final result:
newArray = [{
firstName:"Dan";
lastName:"Chong";
email:"[email protected]";
value:0;
},{
firstName:"Layla";
lastName:"Sng";
email:"[email protected]";
value:2;
}];
My current solution:
const newArray: Contact[] = [];
this.selectedContact.forEach(index => {
newArray.push(this.contacts.find(c => c.value === index));
});