1

This might looks stupid, but i'm lost. I want to Map through countries and regions array. I have a Countries Array that also includes Region's Array. I can access the Countries Array, but when i try to access the Regions Array i returned from the Countries Array, it returns undefined. Here is a minimal example.

Full code on CodeSandBox

const Countries = [
  {
    countryName: "Afghanistan",
    countryShortCode: "AF",
    regions: [
      {
        name: "Badakhshan",
        shortCode: "BDS",
      },
      {
        name: "Badghis",
        shortCode: "BDG",
      },
      {
        name: "Helmand",
        shortCode: "HEL",
      },
      {
        name: "Herat",
        shortCode: "HER",
      },
      {
        name: "Jowzjan",
        shortCode: "JOW",
      },
      {
        name: "Kabul",
        shortCode: "KAB",
      },
      {
        name: "Kandahar",
        shortCode: "KAN",
      }
    ],
  },
  {
    countryName: "Åland Islands",
    countryShortCode: "AX",
    regions: [
      {
        name: "Brändö",
        shortCode: "BR",
      },
      {
        name: "Eckerö",
        shortCode: "EC",
      },
      {
        name: "Sund",
        shortCode: "SD",
      },
      {
        name: "Vårdö",
        shortCode: "VR",
      },
    ],
  }
]

const mappedCountries = Countries.map(({ regions }) => regions);
const mappedRegion = mappedCountries.map(({ name }) => name);

console.log(mappedCountries);
console.log(mappedRegion);

5
  • ok, it seams to be easy, but can you show us the arrays please Commented Nov 19, 2020 at 15:22
  • ah sorry i didnt see the CodeSandBox, sorry Commented Nov 19, 2020 at 15:23
  • mappedCountries is an array of arrays of objects, so you need to iterate deeper. Commented Nov 19, 2020 at 15:27
  • 4
    Please read Something on my web site doesn't work. Can I just paste a link to it?. Questions that depend on external resources to be understood become useless when the external resource goes away or is fixed. Create a minimal reproducible example and put it in the question itself instead. Stackoverflow does support inline live demos Commented Nov 19, 2020 at 15:28
  • @pilchard please avoid adding code found on external sites to SO even if the OP linked to it. You might be in breach of license. It's better to allow OP to make that change. See also When should I make edits to code? Commented Nov 19, 2020 at 16:03

2 Answers 2

1

you are returing an array in the map method. so the result is an array of arrays.

you need to flatten your result. an easy alternative would be using reduce

const mappedCountries = Countries.reduce((memo, { regions }) => [...memo, ...regions], []);
const mappedRegion = mappedCountries.map(({ name }) => name);

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

2 Comments

You nailed it!... Thanks Man!
You can change your condition inside map like this -
1

You can change your condition inside second map like this to access region's name - mappedCountries.map(e => e.map(({name}) => name))

1 Comment

but this will also return an array of arrays, and not just one single array with names in it.

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.