【问题标题】:For loop inside a method using Vuejs使用 Vuejs 的方法中的 For 循环
【发布时间】:2019-09-11 14:55:02
【问题描述】:

我在 Vuejs 中有一个方法可以根据选择更新一组变量。

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]];
    this.chart1.series[3].data = [this.$store.state.selectedcities[0].value[3]];
    this.chart1.series[5].data = [this.$store.state.selectedcities[0].value[5]];
    this.chart1.series[7].data = [this.$store.state.selectedcities[0].value[7]];
    this.chart1.series[8].data = [this.$store.state.selectedcities[0].value[8]];
    this.chart1.series[9].data = [this.$store.state.selectedcities[0].value[9]];
    this.chart1.series[11].data = [this.$store.state.selectedcities[0].value[11]];
    this.chart1.series[12].data = [this.$store.state.selectedcities[0].value[12]];
    this.chart1.series[13].data = [this.$store.state.selectedcities[0].value[13]];
    this.chart1.series[14].data = [this.$store.state.selectedcities[0].value[14]];
    this.chart1.series[16].data = [this.$store.state.selectedcities[0].value[16]];
    this.chart1.series[17].data = [this.$store.state.selectedcities[0].value[17]];
    this.chart1.series[18].data = [this.$store.state.selectedcities[0].value[18]];
    this.chart1.series[20].data = [this.$store.state.selectedcities[0].value[20]];
    this.chart1.series[21].data = [this.$store.state.selectedcities[0].value[21]];
    this.chart1.series[22].data = [this.$store.state.selectedcities[0].value[22]];
}

问题是它看起来相当冗长,所以我想知道我是否可以在那里运行一个 for 循环来遍历这些数字。问题是数字没有排序。我的意思是该系列以{1,2,3,5,7,8,9,11,12,13,14,16,17,18,20,21,22} 运行。

最后我正在寻找的是这样的东西:

methods:{
 updateChart(){
  var n = {1,2,3,5,7,8,9,11,12,13,14,16,17,18,20,21,22}
    for(i in n){
          this.chart1.series[i].data = [this.$store.state.selectedcities[0].value[i]];
            }
          }
        }

我不太确定该怎么做,因为我对 javascript 还很陌生。

编辑:

是否可以使用此方法添加嵌套的 foreach? 例如:

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]];
            })

请注意,k 已添加到公式中,并且 n 是 k 的子级。所以对于每个 k 它应该运行 n 系列。

【问题讨论】:

  • @BoussadjraBrahim 你完全正确。固定。
  • 相差 20 秒 我还 +1 @Félix 回答

标签: javascript vue.js for-loop vuejs2 vue-component


【解决方案1】:

n 应该是一个数组并使用 forEach 方法循环遍历它,例如:

 var n = [1,2,3,5,7,8,9,11,12,13,14,16,17,18,20,21,22]
    n.forEach(i=>{
          this.chart1.series[i].data = [this.$store.state.selectedcities[0].value[i]];
            })

ُ编辑

var m = [1,3,6]
 m.forEach(k=>{
     n.forEach(i=>{
          this.chart[k].series[i].data = 
        [this.$store.state.selectedcities[k].value[i]];
            })

})

【讨论】:

  • 请通过添加 EDIT 部分来编辑您的问题以使内容可读
  • 我已编辑问题以添加嵌套 for 循环。我不确定两个 forEach 是否可行。
  • 你可以将 n 循环嵌入到 k 循环中
  • 你能添加一个例子吗?我试过了,但我可能做错了什么。
【解决方案2】:

你可以使用foreach

updateChart() {
    var n = [1,2,3,5,7,8,9,11,12,13,14,16,17,18,20,21,22];

    n.forEach(number => {
        this.chart1.series[number].data = [this.$store.state.selectedcities[0].value[number]];
    });
}

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 2020-03-23
    • 2022-01-16
    相关资源
    最近更新 更多