Having trouble updating an object in an array of objects. Here is the initial array:
myArray = [
{id: 0, description: "some description", status: "initial status", function: someFunction()},
{id: 1, description: "some description", status: "initial status", function: someFunction()},
{id: 2, description: "some description", status: "initial status", function: someFunction()},
{id: 3, description: "some description", status: "initial status", function: someFunction()}
]
When using .map
and running the function, I get a new object like this:
{id: 2, description: "some description", status: "newStatus", function: someFunction()},
The goal is to update/replace the existing object in the array with the one that has the same id. It's worth mentioning that the id of each object corresponds to its index in the array.
UPDATE:
.map
function being used:
myArray.map(item =>
item.function(x => {
x
? setStatus({ ...item, status: 'newStatus' })
: setStatus({ ...item, status: 'newStatus2' });
})
);
This function includes a callback using Math.random to determine the new status - either newStatus
or newStatus2
. The function within each object triggers upon clicking a button in the UI.