Code Tree
Code Tree

Reputation: 2209

vue array reactivity for a dynamic array push

I want a two dimensional array which will have dynamic key and array of objects pushed to it, as you see I tried this.$set and Vue.set but got no success so far,

data() {
rep_recs: [] , // I want this array reactive..so I can display a table
recs:[] // actual data
},
created() {

    while(cond) {
                   //..some code
                   var y_m_key = `${Y}-${M}`

                   //  Vue.set(this.rep_recs, y_m_key,  [])
                   // this.$set(rep_recs, y_m_key,  [])
                   this.rep_recs[y_m_key] = []


                    for (var i=0; i<this.recs.length; i++) {
                        var rec = this.recs[i]
                        this.rep_recs[y_m_key].push(rec) //this works but not reactive
                    }
    }
}

Upvotes: 1

Views: 1864

Answers (1)

Jacob Goh
Jacob Goh

Reputation: 20845

this should work for you

var rec = this.recs[i]
this.$set(this.rep_recs, y_m_key, [
    ...this.rep_recs[y_m_key],
    rec
]);

This creates a new array everytime, which should solve the reactivity issue

Upvotes: 3

Related Questions