It seems that we are wanting to retrieve an object that matches the given country. We can use map to achieve that.
const array = ['UK', 'Germany', 'Sweden'];
const locations = array.map(function findLocation(country) {
});
There can be several objects, so we use an objects array to store them all.
const objects = [
{text: "Stockton-on-Tees, UK"},
{text: "Twilight Hawker's Market, Perth, Australia"},
{text: "Weihnachtsmarkt Am Kölner Dom, Cologne, Germany"}
];
In the map I want to find the first object that matches my requirements:
const array = ['UK', 'Germany', 'Sweden'];
const locations = array.map(function findLocation(country) {
return objects.find(function includesCountry(object) {
});
});
and that requirement is where the object text matches the country.
const array = ['UK', 'Germany', 'Sweden'];
const locations = array.map(function findLocation(country) {
return objects.find(function includesCountry(object) {
return object.text.includes(country);
});
});
You end up with the locations
array having three items, the first two are objects because they matched, and the last one is appropriately undefined.
[
{text: "Stockton-on-Tees, UK"},
{text: "Weihnachtsmarkt Am Kölner Dom, Cologne, Germany"},
undefined
]