[Vuejs]-How do I properly push an array into a data object in Vue?

1👍

Alternative solution, as per the discussion in the comments on the original question: Instead of using an array, just use an object and take advantage of Object.assign().

data () {
  return {
    pathStats: {}
  }
}

computed: {
  loadDetails() {
    fse.stat('C:/').then((res) => {
      let edited = res.mtime
      let created = res.birthtime
      Object.assign(this.pathStats, {'edited': edited, 'created': created})
    })
  }
}

The method Object.assign() takes 2 or more arguments. The first is the object you want to write values to, and the remaining n-1 objects will write values into the destination object in order. In this case, this.pathStats will receive overriding values from the second object.

This solution also has the advantage of not needing to access array elements since an array is not what was desired.

1👍

One way for this is:

How about having pathStats as an associative array.

data () {
  return {
    pathStats: {}
  }
}

computed: {
  loadDetails() {
    fse.stat('C:/').then((res) => {
      let edited = res.mtime
      let created = res.birthtime
      this.pathStats['edited'] = edited
      this.pathStats['created'] = created
    })
  }
}

And you can it this way.

{{pathStats.edited}}
{{pathStats.created}}

Or else (Not recommended though)

But if you want it only has an arrayList. You may want to get it through indices.

{{pathStats[0][0].edited}}
{{pathStats[0][0].created}}
👤Reck

Leave a comment