0

I have the following code to call variables from state dynamically.

compareClick = () => {
        const cars = ['car_a_', 'car_b_', 'car_c_']
        cars.forEach(element => {
            let [brand, model, variant] = [element + 'brand', element + 'model', element + 'variant'];
            console.log(brand); <== the variable names are correct in the console

            fetch(`http://xx.xx.xx.xx:8000/api/car/${this.state.brand}/${this.state.model}/${this.state.variant}/`)
            .then(response => response.json())
            .then(data => this.setState({
                data : this.state.data.concat(data)
            }))
        });
    }

As you can see I am trying to call the variable names dynamically but it is not working. Any suggestions?

MiniGunnR
  • 5,590
  • 8
  • 42
  • 66

0 Answers0