gpt4 book ai didi

javascript - 在 react 中,如何更新数组状态的特定属性?

转载 作者:行者123 更新时间:2023-12-01 01:46:46 25 4
gpt4 key购买 nike

我有一个像这样的数组的状态:

constructor(props){
super(props)
this.state={
test_steps: []
}
}

它具有以下属性:

test_steps: [
{
description: "Test desc"
expected_results: "Test exp. results"
other_info: "test other info"
status: true
step_number: "1"
_id: {$oid: "1234565894"}
}
]

如果我只想更新此状态的 **other_info** 属性,我将如何实现?

最佳答案

您首先需要获取您的状态并将其存储在变量中。然后循环该值并修改它。之后只需设置状态即可。

let test_steps = this.state.test_steps;
test_steps.map((test)=>{
test.other_info = "YOUR_UPDATED_INFO";
return test;
});

this.setState({test_steps});

关于javascript - 在 react 中,如何更新数组状态的特定属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51886210/

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