Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
Obtenga datos dinámicamente de un objeto en React

Construyo una aplicación en React y tengo este objeto de una 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 } }

En mi código, tengo una lista desplegable donde puedo elegir qué moneda quiero:

 <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>

Cuando elijo otra moneda de mi lista desplegable, trato de obtener la moneda de ese objeto dinámicamente, pero no conozco la sintaxis.

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

0

Puede acceder a las claves de un objeto de dos maneras principales diferentes.

 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

Entonces, básicamente, si tiene el nombre de la clave como "weird key" | "regularKey" y desea acceder a su valor, utilice la notación de paréntesis para acceder al valor. La notación de puntos solo funciona si sabe exactamente cuál es la clave y la clave no contiene caracteres extraños (le gustan los espacios o los guiones (-))

leer más aquí

about 4 years ago · Juan Pablo Isaza Report

0

Puede acceder a elementos en un objeto de forma similar a como lo haría en una matriz con claves con nombre:

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

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!