I have a collection of contacts that I need to filter based on the country code. Specifically, I want to identify contacts whose phone numbers start with any of the selected country codes.
var countries = ['1', '91', '55', '972'];
var allContacts = [
{
id: '9123242135321',
name: 'Harun'
},
{
id: '905366365289',
name: 'Koray'
},
{
id: '135366365277',
name: 'Hayo'
},
{
id: '963923824212',
name: 'Bahaa'
},
{
id: '513324515689',
name: 'Hassan'
}];
I'm searching for an efficient one-line solution without using loops. So far, I've attempted:
allContacts.filter(c => c.id.some(l => countries.includes(l)));
However, this approach assumes the id
parameter is an array and searches the entire number instead of just the beginning part. Is there a more effective way to filter the contacts based on whether their id
starts with any of the values in the countries
array?