Can this.setState update the value in an array object?

    constructor(props) {
        super(props);
        this.state = {
          moreData: [
            {name: "a", value: "1"},
            {name: "b", value: "2"},
            {name: "c", value: "3"}
          ]
        }
    }

how to change the value whose name is a to "10" through this.setState

Mar.05,2021

setState can only change the value of the outermost property.

in your case, you need to manually modify it and then re-assign it.

for example:

changeValue() {
    const data = this.state.moreData.map(v => {
        if (v.name === 'a') {
            v.value = '10'
        }
        return v
    });
    this.setState({ moreData: data })
}
Menu