I haven't worked with Vue.Js in some time so my memory is a little rusty. I seem to have an issue where I unable to modify a data array within a function called within the created hook.
What I'm trying to do is build a multidimensional array with the months of the year as the key, and the data (this.data.data which is a prop) as the value. This is a shortened version of the component. I know this.data.data is returning the array of objects required and console.log(this.fixtures); also outputs the built data.
The code for the project can be seen below.
data: function(){
return {
fixtures: []
}
},
created() {
this.setFixtureMonths(this.data.data);
console.log(this.fixtures);
},
methods: {
setFixtureMonths: function (collection) {
let vm = this;
collection.forEach(function(element) {
let d = new Date(element.date);
let m = d.toLocaleString('default', {month: 'long'}).toLocaleLowerCase();
vm.$set(vm.fixtures, m, element);
});
}
}
However on render of the view and checking the Vue Devtools extention, I notice that fixtures remains empty even thought it is being logged by the console correctly. Looking through the documentation I'm aware the conventional assignment of array doesn't work, so I've resorted to using vm.$set as a means to build the array.
Can someone share some insight for me please on how to get the view to render the fixtures?
Thanks
vm.fixturesis an Array, but it's using the stringmto index into the Array; that won't do anything in JavaScript since Array indices are always numbers. Shouldfixturesbe an Object?fixtures: {}instead to make it an Object.