我的状态值是
this.state = {
content: {
text: {
tag1: {
line: "data1"
}
tag2: {
line: "data2"
}
}
}
}
我如何使用 javascript reduce()
函数来更改 tag1
和 tag2
的 line
的值> 到 "changed text"
?
最佳答案
给你:
this.setState(prevState => {
return {
content: {
...prevState.content,
text: Object.keys(prevState.content.text).reduce((newTexts, key) => {
return {
...newTexts,
[key]: {
line: "changed text"
}
}
}, {})
}
}
});
关于javascript - 如何在 reactJS 的嵌套状态对象中使用 reduce 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56078014/