I'm wondering if there is a way to clear or empty an array of objects, let's say for example i have this array of objects, which i borrow from firebase firestore, i do this with a snapshot, here is the code:
let siteButtonsData = firebase.firestore()
.collection('app_settings')
.doc('dashboard')
.collection('main_panel')
.onSnapshot(doc => {
doc.forEach(doc => {
this.arrObj.push(doc.data())
});
})
Then my array of objects is populated correctly, and it's shown like this:
data() {
return {
arrObj: [
{
action: 'one',
id: 1,
text: 'hello1'
},
{
action: 'two',
id: 2,
text: 'hello2'
},
{
action: 'three',
id: 3,
text: 'hello3'
},
]
}
}
With v-for i iterate over this.buttons to create some dynamic content in my app, but when something changes on my firestore database, and the snapshot updates it, i got duplicates, i was hoping to clear the array of objects before updating from firestore, so i would have no duplicates, but it's not working, so far i have tried:
this.arrObj = [{}]
this.arrObj = [] //This option leaves me with no way to push the array of objects
this.arrObj.length = 0
No matter which of this methods i use, i can't update correctly the array of objects, the first time it does the job well gathering the data from firestore, but once i update the database i got duplicates.
thanks in advance for any help or hint
When working with arrays you always need to make sure that Vue's reactivity system is catching the changes and updates the view. Vue is automatically observing some array mutation methods to make sure to update, them being push(), pop(), shift(), unshift(), splice(), sort() and reverse(). See https://v2.vuejs.org/v2/guide/list.html#Mutation-Methods
As for your example, what should work is then:
let siteButtonsData = firebase.firestore()
.collection('app_settings')
.doc('dashboard')
.collection('main_panel')
.onSnapshot(doc => {
this.arrObj.splice(0, this.arrObj.length);
// I would think this works as well:
// this.arrObj = [];
doc.forEach(doc => {
this.arrObj.push(doc.data())
});
})