[Vuejs]-Looping through an array and copying the modified items into another array returns only repetitions of the last set of modifications

0👍

As noted by @IceMetalPunk in the comments, you’ll need to create a new object with the data you intend to push. I’ve attempted to reproduce the data weekArray contains based on the code you provided, but the values may not be exactly the same as what you have.

const weekArray = [
  {"weekNumber":25,"date":1561359600000},
  {"weekNumber":25,"date":1561446000000},
  {"weekNumber":25,"date":1561532400000},
  {"weekNumber":25,"date":1561618800000},
  {"weekNumber":25,"date":1561705200000},
  {"weekNumber":25,"date":1561791600000},
  {"weekNumber":25,"date":1561878000000}
];

console.log(weekArray);

const availabilityArray = [];
const newArray = [];

for (let i = 0; i < 3; i++) {
  weekArray.forEach(item => {
    //Create a new item with an incremented weekNumber value
    //and a date 7 days after the given date value
    let newItem = {
      "weekNumber": item.weekNumber + i,
      "date": item.date + (i*7*8.64e7)
    };
    newArray.push(newItem);
  });
  availabilityArray.push(...newArray);
}

console.log(availabilityArray)

Leave a comment