I was trying to replace a property of an Object in an array with the spread syntax like this:
const origArray = [
{
"uuid":"first-team-uuid",
"name":"Team 1",
"players": [
"first-team-uuid"
]
},
{
"uuid":"second-team-uuid",
"name":"Team 2",
"players":[]
}
]
const doesNotWork = (prev, index, newName) => [...prev, {...prev[index], name: newName}]
const result1 = doesNotWork(origArray, 0, "Team3")
console.log(result1)
// # I know this works:
const doesWork = (prev, index, newName) => {
let old = [...prev]
old.splice(index, 1, {...prev[index], name: newName});
return old;
}
const result2 = doesWork(origArray, 0, "Team3")
console.log(result2)
I expect reslut1 to be like result2, but I seem to be wrong. I would like to write this in a singe line function and not with the workaround I currently have, if possible.