I have an Array of Object and I want to change the value of the element.
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]);
I'm trying this but doesn't work:
const updateDue = (card) => {
cards.map((element) =>{
if(card != undefined){
if(element.id === card.id){
setCard({...cards, element, cardName: 'hello' })
}
}
})
};
The code I need help is this one: setCard({...cards, element, cardName: 'hello' })
If element id === card id change element cardName to 'hello'