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'
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) } }