Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

53
Vistas
JavaScript Nested Object data access

code I'm using to access dataenter image description herethe picture includes country data from country rest API currencies: USD: name: "United States dollar" symbol: "$"

In this situation, the keyword USD is different for each country, in that case how to access that USD and properties inside it(name, symbol) through currencies property. let currencies = { usd: { name: ' ', symbol: ' ' } }; I used this line to declare variable but this line will update usd for all the countries.

7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

We didn't get a full example but I assume you have something like this. This might explain how to access these values

const data = [
  {
    country: "United States",
    currencies: {
      usd: {
        name: "US Dollar",
        symbol: "$",
      },
    },
  },
  {
    country: "England",
    currencies: {
      usd: {
        name: "Sterling Pound",
        symbol: "£",
      },
    },
  },
  {
    country: "Germany",
    currencies: {
      usd: {
        name: "Euro",
        symbol: "€",
      },
    },
  },
];

First you need to get property you're trying to destructure, let's say the country you're trying to access is the US.

const selectedCountryData = data.find((val) => val.country === "United States");

And then simply extract values by destructuring

const { currencies: { usd:{ name, symbol } } } = selectedCountryData;
7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.