在某一组记录中,我能够从输入字段中获取值,并将它们添加到数组中。现在我需要将这些值赋值给键。我试过一种逻辑,但行不通。请参考下面的代码。
const handleChange = (data) => {
const { id, textVal, dateVal } = data;
setDataNew((prevInfo) => {
const newList = [...prevInfo];
const index = newList.findIndex((datum) => datum.id === id);
if (index !== -1) {
if (textVal !== undefined) {
newList[index].textVal = textVal;
}
if (dateVal !== undefined) {
newList[index].dateVal = dateVal;
}
} else {
newList.push({ id, textVal, dateVal });
}
return [...newList];
});
};在控制台中,我得到数据如下所示

我想要数据的形式
0:
dateData: Fri Sep....
newId: 6705
franchise: "Mike John"
1:
dateData: Sun Sep....
newId: 6703
franchise: "Mark Ray"用新钥匙代替旧钥匙。我试过了
if (textVal !== undefined) {
newList[index].franchise = textVal;
}
if (dateVal !== undefined) {
newList[index].dateData = dateVal;
}但是没起作用..。什么是最好的解决方案?任何建议都会受到高度赞赏。
请参阅代码框链接-> https://codesandbox.io/s/elated-varahamihira-xpjtdb?file=/src/Table.js:856-1029
发布于 2022-09-21 14:31:18
您没有始终如一地重命名属性。这样做的工作是:
const handleChange = (data) => {
const { id: newId, textVal: franchise, dateVal: dateData } = data;
setDataNew((prevInfo) => {
const newList = [...prevInfo];
const index = newList.findIndex((datum) => datum.newId === newId);
if (index !== -1) {
if (newId !== undefined) {
newList[index].newId = newId;
}
if (franchise !== undefined) {
newList[index].franchise = franchise;
}
if (dateData !== undefined) {
newList[index].dateData = dateData;
}
} else {
newList.push({ newId, franchise, dateData });
}
return [...newList];
});
};https://stackoverflow.com/questions/73802346
复制相似问题