Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
Nested object needs to be modified

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda