3

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?

6
  • data.forEach(i => arr.push(i.name + ", " + i.country)) should do it Commented Nov 30, 2020 at 14:54
  • @ibrahimmahrir, noticed just know, thankx Commented Nov 30, 2020 at 15:00
  • data.map(i => arr.push(`${i.name} , ${i.country}`) Commented Nov 30, 2020 at 15:01
  • Demo for my comment Commented Nov 30, 2020 at 15:01
  • @AbishekKumar you are still using map like forEach. That should be a forEach not map. Commented Nov 30, 2020 at 15:03

4 Answers 4

6

Use map like so:

const arr = data.map(city => city.name + ", " + city.country);

arr will be a new array of the same length as data where each city object in data is mapped to the string city.name + ", " + city.country.

Demo:

const data = [ {country: "Andorra", geonameid: 3040051, name: "les Escaldes", subcountry: "Escaldes-Engordany"}, {country: "Andorra", geonameid: 3041563, name: "Andorra la Vella", subcountry: "Andorra la Vella"}, {country: "United Arab Emirates", geonameid: 290594, name: "Umm al Qaywayn", subcountry: "Umm al Qaywayn"}, {country: "United Arab Emirates", geonameid: 291074, name: "Ras al-Khaimah", subcountry: "Raʼs al Khaymah"} ];

const arr = data.map(city => city.name + ", " + city.country);

console.log(arr);

Sign up to request clarification or add additional context in comments.

Comments

3

const data = [{country: "Andorra", geonameid: 3040051, name: "les Escaldes", subcountry: "Escaldes-Engordany"},
{country: "Andorra", geonameid: 3041563, name: "Andorra la Vella", subcountry: "Andorra la Vella"},
{country: "United Arab Emirates", geonameid: 290594, name: "Umm al Qaywayn", subcountry: "Umm al Qaywayn"},
{country: "United Arab Emirates", geonameid: 291074, name: "Ras al-Khaimah", subcountry: "Raʼs al Khaymah"}]

const arr = data.map(i => `${i.name} , ${i.country}`);
console.log(arr)

Comments

2

Simple Solution with forEach

countries = [
  {country: "Andorra", geonameid: 3040051, name: "les Escaldes", subcountry: "Escaldes-Engordany"},
  {country: "Andorra", geonameid: 3041563, name: "Andorra la Vella", subcountry: "Andorra la Vella"},
  {country: "United Arab Emirates", geonameid: 290594, name: "Umm al Qaywayn", subcountry: "Umm al Qaywayn"},
  {country: "United Arab Emirates", geonameid: 291074, name: "Ras al-Khaimah", subcountry: "Raʼs al Khaymah"}
]

let arr = [];

let result = [];

countries.forEach((country, index) => {
    let nameStr = `${country.name}, ${country.country}`
    result.push(nameStr);
})

console.log(result)

//output
//["les Escaldes Andorra", "Andorra la Vella Andorra", "Umm al Qaywayn United Arab Emirates", "Ras al-Khaimah United Arab Emirates"]

Comments

1

You can use reduce function if you want to:

arr.reduce((acc, curr) => [...acc, (`${curr.name}, ${curr.country}`)], []);

It will return your values in your desired format.

Example below.

const arr = [
    {country: 'Andorra', geonameid: 3040051, name: 'les 1', subcountry: 'Escaldes-Engordany'},
    {country: 'Turkey', geonameid: 3040021, name: 'les Escaldes', subcountry: 'aaaa'},
    {country: 'IDK', geonameid: 3040021, name: 'Wow', subcountry: 'aaaa'}
];
const custom = arr.reduce((acc, curr) => [...acc, (`${curr.name}, ${curr.country}`)], []);
console.log(custom);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.