const location = ['Bradenton', 'Broward', 'Miami', 'Orlando', 'Palm Beach']; var url = '?_dFR[location_page][0]=Miami&_dFR[location_page][1]=Orlando&_dFR[make][0]=Acura&_dFR[make][1]=Audi&_dFR[make][2]=BMW&_dFR'; Como puede ver, la url tiene Miami y Orlando que tienen location .
El resultado debería ser: ['Miami', 'Orlando']. Traté de usar expresiones regulares pero es demasiado complejo. hay una manera mas facil?
Creo que estás buscando un filter .
const locationList = ['Bradenton', 'Broward', 'Miami', 'Orlando', 'Palm Beach']; const url = '?_dFR[location_page][0]=Miami&_dFR[location_page][1]=Orlando&_dFR[make][0]=Acura&_dFR[make][1]=Audi&_dFR[make][2]=BMW&_dFR'; const results = locationList.filter(currentLocation => url.includes(currentLocation)); console.log(results); También puede usar toLowerCase() y toUpperCase() para ignorar mayúsculas y minúsculas para sus coincidencias
const locationList = ['Bradenton', 'Broward', 'Miami', 'Orlando', 'Palm Beach']; //lowercase for `orlando` and uppercase for `MIAMI` const url = '?_dFR[location_page][0]=MIAMI&_dFR[location_page][1]=orlando&_dFR[make][0]=Acura&_dFR[make][1]=Audi&_dFR[make][2]=BMW&_dFR'; const results = locationList.filter(currentLocation => (url.includes(currentLocation.toLowerCase()) || url.includes(currentLocation) || url.includes(currentLocation.toUpperCase()))); console.log(results);Versión regular
const locationList = ['Bradenton', 'Broward', 'Miami', 'Orlando', 'Palm Beach']; //lowercase for `orlando` and uppercase for `MIAMI` const url = '?_dFR[location_page][0]=MIAMI&_dFR[location_page][1]=orlando&_dFR[make][0]=Acura&_dFR[make][1]=Audi&_dFR[make][2]=BMW&_dFR'; const results = locationList.filter(currentLocation => new RegExp(currentLocation, 'i').exec(url)); console.log(results); const locations = ['Bradenton', 'Broward', 'Miami', 'Orlando', 'Palm Beach']; var url = '?_dFR[location_page][0]=Miami&_dFR[location_page][1]=Orlando&_dFR[make][0]=Acura&_dFR[make][1]=Audi&_dFR[make][2]=BMW&_dFR'; const foundLocs=locations.filter(l=>url.includes(l)) console.log(foundLocs);