0

I have a nested JSON array that getting from a mongoDB query that i would like to convert into flat JSON .I am using nested mondo documents, but i would like to show the data in a more readble way. My JSON has the following structure:

[{
     "country": "Country A",
     "regions": [{
            "region": "region A1",
            "cities": [{
                    "city": "city A11"
                },
                {
                 "city": "city A12"
                }
            ]
            },
            {
                "region": "region A2",
                "cities": [{
                        "city": "city A21"
                    },
                    {
                        "city": "city A22"
                    }
                ]
            }
        ]
    },
    {
        "country": "Country B",
        "regions": [{
                "region": "region B1",
                "cities": [{
                        "city": "city B11"
                    },
                    {
                        "city": "city B12"
                    }
                ]
            },
            {
                "region": "region B2",
                "cities": [{
                        "city": "city B21"
                    },
                    {
                        "city": "city B22"
                    }
                ]
            }
        ]
    }
]

I want to show only the important information and not the structure of the nested array. How i can modify my data in Javascript on order to achieve the following result.

[
  {
    "country": "Country A",
    "region":"Region A1",
    "city": "City A11"
  },
   {
    "country": "Country A",
    "region":"Region A1",
    "city": "City A12"
  },
  -------------
   {
    "country": "Country B",
    "region":"Region B1",
    "city": "City B11"
  },
  -----------
   {
    "country": "Country B",
    "region":"Region B2",
    "city": "City B22"
  }
]

What is the simpliest way to acheive this result?

1

1 Answer 1

1

The simplest way is to just loop through and create an array. You can do this with reduce():

let arr = [{"country": "Country A","regions": [{"region": "region A1","cities": [{"city": "city A11"},{"city": "city A12"}]},{"region": "region A2","cities": [{"city": "city A21"},{"city": "city A22"}]}]},{"country": "Country B","regions": [{"region": "region B1","cities": [{"city": "city B11"},{"city": "city B12"}]},{"region": "region B2","cities": [{"city": "city B21"},{"city": "city B22"}]}]}]

let flat = arr.reduce((arr, {country, regions}) => {
    regions.forEach(({region, cities}) => {
        cities.forEach(({city}) => {
            arr.push({country, region, city})
        })
    })
    return arr
}, [])
console.log(flat)

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

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.