修改state中数组对象某一个字段(state中某个属性为对象或数组时,需要修改该对象中的某一个属性)
this.state = {
listData: [
{name: "张三", age: "20"},
{name: "李四", age: "21"},
],
obj: {
name: "王五",
age: "22",
}
}
change = (index) => {
const listData = [...this.state.listData]; //复制数组--浅拷贝
const obj = Object.assign({}, this.state.obj, { age: "21" });
this.setState({
listData: listData.map((item, idx) => idx === index ? {...item, name: "赵六"} : item),
obj: obj,
})
}