Javascript / Vue JS - Retrieve value of all elements in object

Viewed 30

I have an object that retrieves 4 different elements with different numerical values. I'm trying to access and retrieve all these numerical values.

The object returns the following:

{__ob__: Observer}
  collectedTrashCount: 139
  dangerousAreaCount: 11
  schoolCount: 5
  trashBinCount: 44

If I want to retrieve the value of the collectedTrashCount, I would simply do the following:

computed: {
    dashboardList: function () {
      return this.$store.getters.getDashboard;
    },
    checkCount: function () {
      console.log(this.dashboardList.collectedTrashCount);
    }
  },

The console.log in this case would give me 139.

My question is: What should I do to return all these values such as: 139, 11, 5, 44?

2 Answers

You could use entries method to map that values in an array :

checkCount: function () {
   return Object.entries(this.dashboardList).map(([key, val]) => val)

}

Another simple way that doesn't require mapping is using Object.values():

const obj = {
  collectedTrashCount: 139,
  dangerousAreaCount: 11,
  schoolCount: 5,
  trashBinCount: 44,
}
const values = Object.values(obj)
console.log(values)

Related