Trying to wrap my brain around how I should tackle filtering an array of objects and only returning results that satisfy all of the tags.
The tags could be any of the fields - fname
/lname
/email
/position
/etc
let search_tags = ['CEO', 'Steven'];
let contacts = [
{ fname: 'Steve', lname: 'Johnson', email: 'user@domain.com', position: 'CEO' },
{ fname: 'James', lname: 'Laurence', email: 'boss@domain.com', position: 'CFO' }
]
let results = contacts.filter((contact) => {
if (search_tags.includes(contact.fname) ||
search_tags.includes(contact.lname) ... ) {
return contact;
}
}
I shortened a bit of the code for brevity and obviously this solution will return contacts that match any search_tag
but... I need to only return results that satisfy every search_tag
.
It's been a long day and I have no one to talk this through so I'm hoping someone may be able to point me in the right direction or give me that ah-ha! moment I'm hoping for :)
Thanks in advance!