Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

54
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs