Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To set an array object in React using JSON if there is no pre-existing value, first initialize the state of the component to an empty array in the constructor like this:

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

Then, in the component's render() method or any other method, you can update the state by setting the array object using JSON syntax like this:

this.setState({
  myArray: [
    { id: 1, name: 'John' },
    { id: 2, name: 'Jane' },
    { id: 3, name: 'Bob' },
  ]
});

This will set the myArray state to an array of objects with three items, each with an id and name property. Note that you can replace the object properties and values with your own data as needed.