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

128
Views
Cómo actualizar el valor en la matriz de objetos en ganchos useState

Soy nuevo en React Hooks . Tengo un state que se ve así.

 const [myData, setmyData] = useState([ { "OrderId": "ord-898s1", "Shoulder Size": "12", "Amount": 200, "Shoulder Width": "2", }, { "OrderId": "ord-898s2", "Shoulder Size": "14", "Amount": 790, "Shoulder Width": "2", } ]) var orderID = "ord-898s2" var AmountToAdd = 100

Quiero actualizar el valor de la Amount en orderID ord-898s2 . Al agregar 100 , el valor de la cantidad en orderID ord-898s2 debe ser 890

 console.log(myData)

Salida requerida:

 [ { "OrderId": "ord-898s1", "Shoulder Size": "12", "Amount": 200, "Shoulder Width": "2", }, { "OrderId": "ord-898s2", "Shoulder Size": "14", "Amount": 890, "Shoulder Width": "2", } ]

Probé estos.

 const result2 = myData.map((item) => { if (item.OrderId === orderID) { return { ...item, ["Amount"]:item.Amount +100 } } return item }) setmyData(result2)

Falló. No sé cómo hacerlo. Por favor, ayúdame con algunas soluciones.

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

0

Podrías hacerlo así:

 setmyData((prev) => prev.map((el) => el.OrderId === "ord-898s2" ? { ...el, Amount: el.Amount + 100 } : el ) );
about 4 years ago · Juan Pablo Isaza Report

0

Pruebe el siguiente código.

 var data = [ { "OrderId": "ord-898s1", "Shoulder Size": "12", "Amount": 200, "Shoulder Width": "2", }, { "OrderId": "ord-898s2", "Shoulder Size": "14", "Amount": 790, "Shoulder Width": "2", } ]; var orderID = "ord-898s2" var AmountToAdd = 100 data.forEach((item) => item.OrderId === orderID ? item.Amount = item.Amount + AmountToAdd : item.Amount); console.log(data);

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!