gpt4 book ai didi

javascript - 如何在 reactJS 的嵌套状态对象中使用 reduce 函数?

转载 作者:行者123 更新时间:2023-11-29 20:39:00 25 4
gpt4 key购买 nike

我的状态值是

this.state = {
content: {
text: {
tag1: {
line: "data1"
}
tag2: {
line: "data2"
}
}
}
}

我如何使用 javascript reduce() 函数来更改 tag1tag2line 的值> 到 "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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com