数组中的展开操作出错。 TS1005:“,”预期。打字稿
Error with spread operation in array. TS1005: ',' expected. TypeScript
我无法弄清楚我在 row.sections[SECTION_ID
线上错过了什么。它总是向我显示拼写错误 ','...
FAQ: sections
- is an array with objects
inside. In this case I'm
trying modify the specific object of the sections founded by custom
flag SECTION_ID.
P.S.
我也尝试将 row.sections[SECTION_ID]
放在额外的括号 []
中,但不幸的是它没有帮助...有什么解决方案吗?
rows: state.rows.map(
row =>
row.ID === action.rowID
? {
...row,
sections: [
...row.sections,
row.sections[SECTION_ID]: { // error is here
...row.sections[SECTION_ID],
data: {
...// some data
}
}
]
}
: row
)
如果您试图在不改变数组的情况下替换数组特定索引处的元素,您可以制作数组的浅表副本,然后在该索引处设置值。例如:
state.rows.map((row) => {
if (rowID !== action.rowID) {
return row;
}
const sectionCopy = [...row.sections];
sectionCopy[SECTION_ID] = {
...row.sections[SECTION_ID],
data: {
// some data
},
};
return {
...row,
sections: sectionCopy,
};
});
你不能用这种方式通过spread
操作来改变array
里面的一些element
。使用这种方法,您每次只需将一个新的、变异的 element
添加到相同的 array
。所以,如果你想让它正确,你需要使用 map
迭代器来代替:
rows: state.mymaps.rows.map(
row =>
row.ID === action.rowID
? {
...row,
sections: row.sections.map(
(section, index) =>
index === JOIN_SECTION_ID
? {
...section,
data: {
...section.data
}
} : section
)
} : row
)
我无法弄清楚我在 row.sections[SECTION_ID
线上错过了什么。它总是向我显示拼写错误 ','...
FAQ:
sections
- is an array withobjects
inside. In this case I'm trying modify the specific object of the sections founded by custom flag SECTION_ID.
P.S.
我也尝试将 row.sections[SECTION_ID]
放在额外的括号 []
中,但不幸的是它没有帮助...有什么解决方案吗?
rows: state.rows.map(
row =>
row.ID === action.rowID
? {
...row,
sections: [
...row.sections,
row.sections[SECTION_ID]: { // error is here
...row.sections[SECTION_ID],
data: {
...// some data
}
}
]
}
: row
)
如果您试图在不改变数组的情况下替换数组特定索引处的元素,您可以制作数组的浅表副本,然后在该索引处设置值。例如:
state.rows.map((row) => {
if (rowID !== action.rowID) {
return row;
}
const sectionCopy = [...row.sections];
sectionCopy[SECTION_ID] = {
...row.sections[SECTION_ID],
data: {
// some data
},
};
return {
...row,
sections: sectionCopy,
};
});
你不能用这种方式通过spread
操作来改变array
里面的一些element
。使用这种方法,您每次只需将一个新的、变异的 element
添加到相同的 array
。所以,如果你想让它正确,你需要使用 map
迭代器来代替:
rows: state.mymaps.rows.map(
row =>
row.ID === action.rowID
? {
...row,
sections: row.sections.map(
(section, index) =>
index === JOIN_SECTION_ID
? {
...section,
data: {
...section.data
}
} : section
)
} : row
)