0

I want to set the state of my component after a certain amount time but the problem is that the state is an array nested in an object nested in an array. Here is my code:

this.state = {
items: {
    label: ['A', 'B', 'C', 'D'],
    data: [
        {
            label: 'A',
            value: [1, 2, 3, 4, 5]
        }
      ]
    }
  }

I want to change the state of value to the following:

value: ["a", "b", "c", "d"]

How can I achieve this?

Constantin Groß
  • 10,719
  • 4
  • 24
  • 50
Vikas Singh
  • 1,791
  • 1
  • 14
  • 26

1 Answers1

-1

You can achieve this like this:

this.state.items.data[0].value = ["a", "b", "c", "d"];
this.setState({items: this.state.items });

If you want something to update which is nested. First update the value directly in the state obj (line 1) and then do setState for the state variable(line2)

Arpit
  • 438
  • 1
  • 6
  • 15