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

158
Views
¿Cómo mapear la propiedad y el valor de un objeto a una tabla?

Este es mi código hasta ahora, extrayendo datos de moneda históricos basados en una tasa 'base' y una fecha seleccionada. ¿Hay alguna manera de mostrar los datos devueltos en las columnas 'nombre de moneda' y 'tasa'? Mi código hasta ahora está abajo y aquí: http://codepen.io/co851002/pen/PWqVwr

 var date = '2017-01-06' var base = 'USD' var API_request = 'https://api.fixer.io/' + date + '?base=' + base; var App = React.createClass({ getInitialState: function() { return { rates: [] } }, componentDidMount: function() { var th = this; this.serverRequest = axios.get(API_request) .then(function(result) { console.log(result.data.rates) th.setState({ rates: result.data.rates }); }) }, componentWillUnmount: function() { this.serverRequest.abort(); }, render: function() { var self = this; return ( <div className="table"> <table > <thead> <tr> <th>Currency</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>Currency goes here</td> <td>Value goes here</td> </tr> </tbody> </table> </div> ) } }); React.render(<App />, document.querySelector("#root"));
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Simplemente use Object.keys(rates) para .map sobre sus datos y devuelva los elementos DOM deseados en cada iteración.

Objeto.claves()

El método Object.keys() devuelve una matriz de propiedades enumerables propias de un objeto determinado...

En otras palabras, Object.keys le dará una matriz con todos los diferentes tipos de moneda con un aspecto similar a este:

 ["AUD", "BGN", "BRL", ...]

Ahora puede usar Array.prototype.map para iterar sobre la matriz y devolver lo que sea necesario devolver.

matriz.prototipo.mapa()

El método map() crea una nueva matriz con los resultados de llamar a una función proporcionada en cada elemento de esta matriz.

 {Object.keys(rates).map(function(value, idx) { return <tr key={idx}> <td>{value}</td> <td>{rates[value]}</td> </tr> })}

Aquí hay un ejemplo.

 var date = '2017-01-06' var base = 'USD' var API_request = 'https://api.fixer.io/' + date + '?base=' + base; var App = React.createClass({ getInitialState: function() { return { rates: [] } }, componentDidMount: function() { var th = this; this.serverRequest = axios.get(API_request) .then(function(result) { console.log(result.data.rates) th.setState({ rates: result.data.rates }); }) }, componentWillUnmount: function() { this.serverRequest.abort(); }, render: function() { var self = this; var rates = this.state.rates; return ( <div className="table"> <table> <thead> <tr> <th>Currency</th> <th>Value</th> </tr> </thead> <tbody> {Object.keys(rates).map(function(value, idx) { return <tr key={idx}> <td>{value}</td> <td>{rates[value]}</td> </tr> })} </tbody> </table> </div> ) } }); ReactDOM.render(<App />, document.querySelector("#root"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/axios/0.8.1/axios.min.js"></script> <div id="root"></div>

over 4 years ago · Santiago Trujillo 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!