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

170
Views
¿Cómo cambio el valor de un objeto dentro de una matriz?

Tengo una matriz de objetos y quiero cambiar el valor del elemento.

 const cardObj = [ { id: randomNum(), cardName: "My Best Buy", totalCreditLine: 1000, currentBalance: 400, monthlyPayment: 100, dueBy: "2022-4-18" }, { id: randomNum(), cardName: "Amazon", totalCreditLine: 2000, currentBalance: 1200, monthlyPayment: 200, dueBy: "2022-4-10" }, { id: randomNum(), cardName: "Home Depot", totalCreditLine: 1500, currentBalance: 800, monthlyPayment: 300, dueBy: "2022-4-15" }, { id: randomNum(), cardName: "Ebay", totalCreditLine: 500, currentBalance: 300, monthlyPayment: 100, dueBy: "2022-3-30" } ]; const [cards, setCard] = useState([...cardObj]);

Estoy intentando esto pero no funciona:

 const updateDue = (card) => { cards.map((element) =>{ if(card != undefined){ if(element.id === card.id){ setCard({...cards, element, cardName: 'hello' }) } } }) };

El código que necesito ayuda es este: setCard({...cards, element, cardName: 'hello' })

Si la identificación del elemento === identificación de la tarjeta, cambie el nombre de la tarjeta del elemento a 'hola'

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

0

Debe cambiar toda la matriz y luego establecer el valor

 const updateDue = (card) => { if (card) { const updatedCard = cards.map(element => { if (card.id === element.id) { return { ...card, cardName: 'hello' } } else { return { ...card } } }) setCard(updatedCard) } }
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!