0

Using Vue.js 2. I have a list of objects I'm displaying to the user. An entity of that object specifically. Here's a screen of the object (only showing 1 for brevity) and the list it's displayed in

enter image description here

<v-sheet elevation="10" rounded class="py-4 px-1">
      <div class="text-center text-caption">
        Device Serial Number {{ this.deviceOptions.length }}
           </div>
             <v-chip-group
                multiple
                active-class="primary--text"
                v-model="deviceOptions"
              >
             <v-chip
                v-for="item in $store.state.responeArray"
                  :key="item.key"
                  @click="countDevices(item)" // pass this selected object to a method
             >
                  {{ item.DeviceSerialNumber }}
                </v-chip>
            </v-chip-group>
 </v-sheet>

enter image description here

When one of these items are clicked I pass the item object to a method where I add the CollectionId to an array if it doesn't exist.

The object gets passed correctly, and I check if it exists in the array. If it doesn't, I add it. If I deselect, I check and remove it. here's that method.

countDevices(e) {
      if (this.deviceOptions.indexOf(e.CollectionId) >= 0) {
        for(let i = 0; i < this.deviceOptions.length; i++) {
          if (this.deviceOptions[i] === e.CollectionId){
            this.deviceOptions.splice(i, 1);
            
          }
        }

        console.log(this.deviceOptions.length);
        console.log(this.deviceOptions);
      } else {
        this.deviceOptions.unshift(e.CollectionId);
        console.log(this.deviceOptions.length);
        console.log(this.deviceOptions);
      }
    },

The actual problem

However, when you add to the array, it adds two values. not just one. I can't tell what that extra value is, but I think it's the observer vue uses.

Take notice of the length displayed that console where the value is present and observer

enter image description here

There's only one item selected, but length is showing 2. Whereas my goal is to show the counted number of indexes within the array.

Can anyone assist in this?

enter image description here

swapmeet_Lou
  • 282
  • 2
  • 22
  • Is there anywhere else that you add elements to or otherwise populate `this.deviceOptions` ? – James Apr 01 '22 at 15:45
  • @ James. Great question, but there is not. I only use it there to add/remove those values. – swapmeet_Lou Apr 01 '22 at 15:49
  • So if you had console.log(this.deviceOptions) at the top of countDevices, it shows an empty array on the first click? – James Apr 01 '22 at 15:54
  • Yes. I'll edit and add a screenshot so you can see.. I log out the this.deviceOptions at the top of the method and this is what is shown. – swapmeet_Lou Apr 01 '22 at 15:58
  • Hm, but the "empty" array already has the observer in it. [this question](https://stackoverflow.com/questions/52873516/vue-js-returns-ob-observer-data-instead-of-my-array-of-objects) mentions a similar phenomenon, but as the result of an unfinished async process (so "__ob__" is a placeholder for data that has not finished loading yet) – James Apr 01 '22 at 16:03
  • That's odd. This object is already returned from the API and sitting in a vuex store array. Hence the list I'm displaying in. deviceOption is an array I create locally on the component and solely use to count how many of those icons are clicked. I guess I'm saying is by this point I'm no longer waiting for an async method to finish. – swapmeet_Lou Apr 01 '22 at 16:06

0 Answers0