Beginner here - I'd like to create an array for my cities of the world type ahead project from a JSON file. I've fetched the data, now I would like to extract 2 key/value pairs (called name/country) and push them into a new string array that will have the syntax:
const arr = ["name, country", "name, country", "name, country"...]
Here is a sample of the data:
[]:
0: {country: "Andorra", geonameid: 3040051, name: "les Escaldes", subcountry: "Escaldes-Engordany"}
1: {country: "Andorra", geonameid: 3041563, name: "Andorra la Vella", subcountry: "Andorra la Vella"}
2: {country: "United Arab Emirates", geonameid: 290594, name: "Umm al Qaywayn", subcountry: "Umm al Qaywayn"}
3: {country: "United Arab Emirates", geonameid: 291074, name: "Ras al-Khaimah", subcountry: "Raʼs al Khaymah"}...
I know I have to use .push() and .forEach() or a for loop but I'm not sure how to go about it. Can anyone show me how?
data.forEach(i => arr.push(i.name + ", " + i.country))should do itdata.map(i => arr.push(`${i.name} , ${i.country}`)maplikeforEach. That should be aforEachnotmap.