Home  >  Q&A  >  body text

How to loop over an array in data()

I want to iterate an array in javascript inside vue.

I'm using a vertex graph. I want to iterate data[] based on the number of series (Y_Data_length).

I want to change the 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: [],
          }
        ],
      };
    },

Form it.

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

For reference only, Y_Data_length is:

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

P粉038161873P粉038161873189 days ago440

reply all(1)I'll reply

  • P粉434996845

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

    I'm not sure if I understand your question correctly, but if you want to get an array of data from a specific series, you can use Vue "compute" to use Y_Data_length as an array The index automatically gets the correct series.data. Whenever Y_Data_length changes, this.currentSeriesData is also updated.

    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 []
        }
      }
    }
    

    reply
    0
  • Cancelreply