Within Vuejs, I have a method that updates multiple variables based on user selection.
methods: {
updateChart(){
this.chart1.series[1].data = [this.$store.state.selectedcities[0].value[1]];
this.chart1.series[2].data = [this.$store.state.selectedcities[0].value[2]];
// Update the remaining series with similar pattern...
}
The current code seems quite verbose. I'm considering using a for loop to iterate through the numbers, even though they are not sequential as indicated by
{1,2,3,5,7,8,9,11,12,13,14,16,17,18,20,21,22}
.
I would like to achieve something like:
methods:{
updateChart(){
var n = [1, 2, 3, 5, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 20, 21, 22];
for(let i of n){
this.chart1.series[i].data = [this.$store.state.selectedcities[0].value[i]];
}
}
}
As a newbie in JavaScript, I'm unsure about how to proceed with this approach.
EDIT:
Is it feasible to include a nested forEach loop in this scenario? For instance:
var k = [1, 3, 6];
var n = [1, 2, 3, 5, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 20, 21, 22];
n.forEach(i => {
this.chart[k].series[i].data = [this.$store.state.selectedcities[k].value[i]];
})
Note the addition of variable k into the formula and the relation of n as a subset of k. This implementation aims to run series of n for each k value.