I assigned two variables to the array data obtained by the API in vue, and then one of the variables, push, added a value, but found that the other variable also increased.

I assigned two variables to the array data obtained by the API in

vue, and then one of them added a value through push, but found that the other variable also changed, as follows:

apiGetDeviceType().then(response => {
                    let data=response.data
                    this.deviceType1 =data
                    this.deviceType2 =data
                  
                    this.deviceType1.push({
                        name: "",
                        code: "all"
                    })
                })

I don"t know why

Feb.06,2022

because both your this.deviceType1 and this.deviceType2 are pointers to response.data, not two independent variables, but two identical indexes, so when you modify the properties of which one, the other will change as well. Copy response.data with json.parse (json.stringify (response.data))


the basics.. Juvenile

here data is an object, and in js it is a reference type, which is not given a value, but the address of the reference

for example, demo

   

Menu