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

363
Views
Iterar objeto y reemplazar valores

Tengo este Objeto:

 { "data": { "success": true, "historical": true, "date": "2022-01-01", "base": "MXN", "rates": { "COFFEE": 0.02158734144632395, "CORN": 0.008232645172711363, "COTTON": 0.04320921676820366, "SOYBEAN": 0.0036714622235960175, "SUGAR": 0.25680398615582695, "WHEAT": 0.00017592643558262669 }, "unit": "per bushel" } }

Y quiero iterar sobre "tasas" para reemplazar los valores de cada clave con 1/valor
Probé con: (prueba es el nombre del Objeto)

 Object.values(this.prueba.data.rates).forEach((val) => { console.log(val) val = 1 / val; console.log(val) })

Pero, ¿cómo reemplazar esos valores o cómo puedo guardarlos en otra matriz u objeto?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Su código no funciona porque el cambio que realizó en val solo se refleja dentro del alcance de la devolución de llamada.

En su lugar, debe recorrer cada propiedad y establecer su valor.

 const obj={data:{success:!0,historical:!0,date:"2022-01-01",base:"MXN",rates:{COFFEE:.02158734144632395,CORN:.008232645172711363,COTTON:.04320921676820366,SOYBEAN:.0036714622235960175,SUGAR:.25680398615582695,WHEAT:.00017592643558262669},unit:"per bushel"}}; let r = obj.data.rates; Object.keys(r).forEach(e => r[e] = 1 / r[e]) console.log(obj)

Si está utilizando Typescript, es necesario declarar el objeto así:

 const obj: {[key: string]: any} = ...
about 4 years ago · Juan Pablo Isaza Report

0

Puede iterar sobre Object.entries (u Object.keys ) y reemplazar el valor de cada clave.

 let obj={data:{success:!0,historical:!0,date:"2022-01-01",base:"MXN",rates:{COFFEE:.02158734144632395,CORN:.008232645172711363,COTTON:.04320921676820366,SOYBEAN:.0036714622235960175,SUGAR:.25680398615582695,WHEAT:.00017592643558262669},unit:"per bushel"}}; Object.entries(obj.data.rates).forEach(([k, v]) => obj.data.rates[k] = 1 / v); console.log(obj);

about 4 years ago · Juan Pablo Isaza Report

0

Creo que será más limpio escribirlo explícitamente usando bucles for:

 let obj = {} //original object stated in question let obj2 = Object.create(null);//DON'T just use {}, see below for(let productName in obj.data.rates){ let inverse = 1/obj.data.rates[productName]; //to edit the object directly obj.data.rates[productName] = inverse; //to add to another object obj2[productName] = inverse; }

La diferencia entre {} y Object.create(null) se puede encontrar aquí

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!