[Vuejs]-Modifying properties fetched with Apollo GraphQL in vueJs

0👍

When you do var out = this.events the out var is now pointing to the same array of objects as this.events.

So if this.events is read-only, you’ll need to make a copy of the array and if each object in the array is read-only you’ll need to make a copy of each object as well.

Here’s an example using the spread operator to copy the array and objects:

computed: {
  eventsFormatted() {
    let out = [...this.events];
    for (let i in out) {
      let item = {...out[i]};
      item.startDate = moment(item.startDate);
      item.endDate = moment(item.endDate);
      out[i] = item;
    }
    return out;
  }
}

The above makes a shallow copy of each object in the array. If you need to make a deep copy because of the structure of this.events, see this post.

Leave a comment