简体   繁体   English

如何在 data() 中循环数组

[英]How can I loop array in data()

First of all, I don't speak English, so I'm using a translator.首先,我不会说英语,所以我正在使用翻译。 Sorry.对不起。 I want to iterate through an array in javascript inside vue.我想遍历 vue 中 javascript 中的一个数组。

I am using apex chart.我正在使用顶点图表。 I want to iterate over data[] according to the number of series(Y_Data_length).我想根据系列数(Y_Data_length)迭代数据[]。

I want to change code我想更改代码

data() {
      return {
        Y_Data_length: null,
        Options: {
          xaxis: {
            categories: [],
          },
        },
        Series_1: [{
          name: "",
          data: [],
        }],

        Series_2: [{
            name: "",
            data: [],
          },
          {
            name: "",
            data: [],
          }
        ],

        Series_3: [{
            name: "",
            data: [],
          },
          {
            name: "",
            data: [],
          },
          {
            name: "",
            data: [],
          }
        ],
      };
    },

to form it.形成它。

data() {
      return {
        Y_Data_length: null,
        Options: {
          xaxis: {
            categories: [],
          },
        },
        Series: [
          {name:"", data: []}
        ],
      };
    },

For reference, Y_Data_length is:作为参考,Y_Data_length 为:

const A = this.chart[0].data
this.Y_Data_length = Object.keys(A).length

I'm not sure to have understand correctly your problem but if you want to get the data array from a specific series, you can use a Vue "computed" to automatically get the correct series.data using Y_Data_length as an array index.我不确定是否正确理解了您的问题,但是如果您想从特定系列中获取data数组,您可以使用 Vue“计算”使用Y_Data_length作为数组索引自动获取正确的 series.data。 Whenever Y_Data_length changes, then this.currentSeriesData will updates too.每当Y_Data_length改变时, this.currentSeriesData也会更新。

export default {
  data () {
    return {
      Y_Data_length: null,
      Options: {
        xaxis: {
          categories: [],
        },
      },
      Series: [
        { name:"series1", data: [] },
        { name:"series2", data: [] },
        { name:"series3", data: [] },
      ],
    };
  },
  computed: {
    currentSeriesData() {
       const currentSeries = this.Series[this.Y_Data_length]
       if (currentSeries) {
         return currentSeries.data
       }
       return []
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM