[Vuejs]-Last iteration of forloop modify final output in vue

2👍

Given your code, it is normal that in the end new1 contains the array of the last iteration.

You should try replacing new1 = item1; with new1 = new1.concat(item1);

For more details on the methods of the Array prototype, I recommend taking a look at the doc on Mozilla Developer Network.

👤ErikMD

0👍

You are replacing the array at each iteration, but you should concatenate them.

methods: {

  getTotals() {
    var self = this;
    var new1 = [];
    this.$http.get('http://localhost:3000/api/purchases')
      .then(function(response) {
        console.log("response.data value")
        console.log(response.data)
        for (var i = 0; i < response.data.length; i++) {
          var item1 = JSON.parse(response.data[i].pur_items);
          console.log("item1 for index i time" + i)
          console.log(item1)

          new1 = new1.concat(item1)

        }
        console.log("final output")
        console.log(new1)


      })

  },

},
👤bugs

Leave a comment