重写数组中的对象



我有一个项目列表。每个项目有2个输入,一个用户可以更改数字,第二个是复选框。这是每个输入的功能:

//this is the user array
const user = [{
id: '',
votes: 0,
active: false
}
];

//this is the function for changing checked status
const checked = (checked, id) => {
user[id] = {
id: id,
votes: how to add here the `votes` state,
active: checked
}
}

//this is the function for changing number
const number = (votes, id) => {
user[id] = {
id: id,
votes: votes,
active: how to add here the `active` state ?,
}
}

问题:当我更改数字时,我会添加投票和id,但我没有来自用户的active值,checked也有同样的问题,我需要添加votes
如何解决这个问题?

只需直接更改所选键的值,如user[id].active=...

//this is the user array
const user = [{
id: '',
votes: 0,
active: false
}
];

//this is the function for changing checked status
const checked = (checked, id) => {
user[id].active = checked;
}

//this is the function for changing number
const number = (votes, id) => {
user[id].votes = votes;
}

最新更新