[Vuejs]-How to get the length of a property in vuejs

1👍

You can filter array and get length:

const obj = { "orders": [{ "status": "delivered" }, { "status": "delivered" }, { "status": "cancelled" }, { "status": "on Delivery" }]}
const res = obj.orders.filter(o => o.status === 'delivered').length
console.log(res)

0👍

At first, you provide an unformatted JSON.
I suppose you mean got handle this array:

const orderArray = [
      { "status": "delivered" }, 
      { "status": "delivered" }, 
      { "status": "cancelled" }, 
      { "status": "on Delivery" }
    ]

const lengthOfDelivered = 
orderArray.filter((order)=>order.status=='delivered').length

Furthermore, if you want to counting others status into a table:

let countTable = []
orderArray.forEach(order => {
    const idx = countTable.findIndex(item => item.status === order.status)
    if (idx !== -1) {
        countTable[idx]['count'] += 1
    } else {
        countTable.push({...order, count: 1})
    }
})
console.log(countTable);

Leave a comment