[英]Error with spread operation in array. TS1005: ',' expected. TypeScript
I cannot figure out what I missed on line row.sections[SECTION_ID
. 我无法弄清在
row.sections[SECTION_ID
上错过了什么。 It always show me a typo error ','... 它总是向我显示错字错误','...
FAQ:
sections
- is an array withobjects
inside.常见问题解答:
sections
是一个内部包含objects
的数组。 In this case I'm trying modify the specific object of the sections founded by custom flag SECTION_ID.在这种情况下,我尝试修改由自定义标记SECTION_ID创建的部分的特定对象。
PS PS
I also tried to put row.sections[SECTION_ID]
inside an extra brackets []
, but unfortunately it does not help... Any solutions? 我也尝试将
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
)
You cannot mutate some element
inside array
by spread
operation in such way. 您无法通过
spread
操作以这种方式对array
内的某些element
突变。 Using this approach you'll jus add a new, mutated element
to the same array
each time. 使用这种方法,您将每次都将一个新的,变异的
element
添加到同一array
。 So, it you want to make it right, you need to use the map
iterator instead: 因此,如果您想使其正确,则需要使用
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
)
If you're trying to replace an element at a certain index of the array without mutating the array, you can make a shallow copy of the array, and then set the value at that index. 如果您试图替换数组某个索引处的元素而不更改数组,则可以对数组进行浅表复制,然后在该索引处设置值。 For example:
例如:
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,
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.