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

141
Visualizações
Get dynamically data from an object in React

I build an app in React and I have this object from an API:

let currency = {
"success": true,
"timestamp": 1648656784,
"base": "EUR",
"date": "2022-03-30",
"rates": {
"AED": 4.09826,
"AFN": 98.736662,
"RON": 4.947456,
"USD": 1.11575,
"GBP": 0.848656
 }
}

In my code, I have a dropdown list where I can choose what currency I want:

                <select name='currencyOptions' onChange={(e) => showCurrency(e.target.value)}>
                  <option defaultValue='USD'>USD</option>
                  <option defaultValue="EUR">EUR</option>
                  <option defaultValue='RON'>RON</option>
                  <option defaultValue='GBP'>GBP</option>
                </select>

When I choose another currency from my dropdown list, I try to get the currency from that object dynamically, but I don't know the syntax.

  function showCurrency(curr) {
    let products_list = [...products];

    for (let i = 0; i < products_list.length; i ++) {
      if(currentCurrency === 'EUR') {
        products_list[i].price = products_list[i].price * currency[0].rates. ????;
      } else {
        console.log(currency[0].rates.curr);
        products_list[i].price = products_list[i].price / currency[0].rates. ????;
      }
    }
  }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can access keys to an object in two different main ways.

const obj = {
    regularKey: "regularValue",
    "weird key": "weirdValue",
}

console.log(obj.regularKey) // "regularValue"
console.log(obj["regularKey"]) // "regularValue" same as above

console.log(obj.weird key) // <- ERROR
console.log(obj["weird key"]) // "weirdValue" This Works

So basically if you have the key name like "weird key" | "regularKey" and you want to access its value use bracket notation to access the value. Dot notation only works if you know exactly what the key is and the key doesn't contain any weird characters (likes spaces or dashes (-))

read more here

about 4 years ago · Juan Pablo Isaza Relatório

0

You can access items in an object similar to how you would in an array with named keys:

let currency = {
  "success": true,
  "timestamp": 1648656784,
  "base": "EUR",
  "date": "2022-03-30",
  "rates": {
    "AED": 4.09826,
    "AFN": 98.736662,
    "RON": 4.947456,
    "USD": 1.11575,
    "GBP": 0.848656
  }
}

// Directly
let gbpValue = currency.rates["GBP"];
console.log(gbpValue);

// Using another var
let currencyKey = "AED";
let aedValue = currency.rates[currencyKey];
console.log(aedValue);

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