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

   

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b400f0-2c4fa.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b400f0-2c4fa.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?