首页  >  问答  >  正文

如何在data()中循环数组

我想在 vue 内的 javascript 中迭代数组。

我正在使用顶点图。我想根据系列数(Y_Data_length)迭代 data[]。

我想更改代码

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: [],
          }
        ],
      };
    },

形成它。

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

仅供参考,Y_Data_length 为:

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

P粉038161873P粉038161873189 天前438

全部回复(1)我来回复

  • P粉434996845

    P粉4349968452024-03-31 10:11:50

    我不确定是否正确理解了你的问题,但如果你想从特定系列中获取 data 数组,你可以使用 Vue“计算”来使用 Y_Data_length 作为数组索引自动获取正确的series.data 。每当 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 []
        }
      }
    }
    

    回复
    0
  • 取消回复