0

This is the input json which i am reading and trying to change the structure as per requirement .But unable to do so countries i am able to put inside object but timezone i am unable to read .Can any one help clearly i need to work on objects little harder .

let timezone = {
  countries: {
    Algeria: {
      cities: {
        "Algiers": {
          city: "Algiers",
          timezone: "(UTC+01:00) W. central africa standard time",
          ianaTz: "Africa/Algiers",
        },
      },
    },
    Argentina: {
      cities: {
        "Buenos aires": {
          city: "Buenos aires",
          timezone: "(UTC-03:00) Argentina standard time",
          ianaTz: "America/Argentina/Buenos_Aires",
        },
        "Cordoba": {
          city: "Cordoba",
          timezone: "(UTC-03:00) Argentina standard time",
          ianaTz: "America/Argentina/Cordoba",
        },
        "Tucuman": {
          city: "Tucuman",
          timezone: "(UTC-03:00) Argentina standard time",
          ianaTz: "America/Argentina/Tucuman",
        },
      },
    }
  }
}


function editTimezone(timezone) {
  var arr = [];

  for (var key in timezone.countries) {
    var city = timezone.countries[key].cities;
    for (var cit in city) {
      var timezone = timezone.countries[key].cities.cit.timezone; //undefined
    }
    arr.push({
      "country": key,
      "timezone": [timezone]
    })
  }
  console.log(arr);
}

editTimezone(timezone);

//OUTPUT REQUIRED--

[{
"country":"Algeria"
"timezone": ["((UTC+01:00) W. central africa standard time"]
 },
 {   
  "country":"Argentina"
  "timezone": ["(UTC-03:00) Argentina standard time","(UTC-03:00) Argentina standard 
  time","(UTC-03:00) Argentina standard time"]
 }
]

Not sure what logic i should write for achieving the output.

miranal
  • 37
  • 6
  • 5
    Please note that `let timezone = {` means the data is not JSON, but a regular JS object. – evolutionxbox Apr 29 '22 at 07:13
  • 1
    **What** is the problem with the `editTimezone` function? You did not say **what** is the actual problem... – vsync Apr 29 '22 at 07:15
  • You're going to need something like `Object.values(country.cities)[0].timezone`. –  Apr 29 '22 at 07:17
  • You need `[cit]` to use the key stored in `cit`, otherwise you're looking for a literal "cit" key, which doesn't exist. Also note that you can use a `for ... of` loop as your 2nd loop instead which allows you to skip over they key step and refer to the value directly. –  Apr 29 '22 at 07:20
  • Does this answer your question? [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) –  Apr 29 '22 at 07:21
  • 1
    There isn't any trace of JSON in this question. [JSON](https://json.org) is text, that in various programming languages is handled by the type `string`. There could not be a thing like _"nested JSON"_. You have nested objects. JSON is a text representation of a data structure (that can be an object or an array or something else, a number for example). – axiac Apr 29 '22 at 07:44

1 Answers1

0

Script:

function editTimezone() {
  var countries = timezone.countries;
  var arr = Object.keys(countries).map(country => {
    var cities = countries[country].cities;
    return {
      'country': country,
      'timezone' : Object.keys(cities).map(city => cities[city].timezone)
    }
  });

  console.log(arr);
}

Steps:

  • First, get the countries, then map them to create an array.
  • Next, get the list of cities per country then map it again with their own timezones.
  • Lastly, return both the country name and the timezones as an object altogether to get an array of these objects.

Output:

output

NightEye
  • 10,634
  • 2
  • 5
  • 24