Peter
Peter

Reputation: 771

Count items in vue object

I like to know how many items have the value material_delivery is 1?

I like to do it in a vue method.

[{
    "id": 43,
    "uuid": "c92421d0-71cc-433d-b7b5-fc6c91c2a3a4",
    "project_id": 8,
    "name": "Konstruktionsfreigabe",
    "due_date": "2019-10-18",
    "material_delivery": 0,
    "closed_at": null,
    "closed_from": null,
    "deleted_at": null,
    "created_at": "2019-12-30 16:43:04",
    "updated_at": "2019-12-31 13:09:41"
}, {
    "id": 44,
    "uuid": "a063964b-f8fc-4c28-9055-09ed5fc4b8dd",
    "project_id": 8,
    "name": "Material",
    "due_date": "2019-12-13",
    "material_delivery": 1,
    "closed_at": null,
    "closed_from": null,
    "deleted_at": null,
    "created_at": "2019-12-30 16:43:04",
    "updated_at": "2019-12-31 13:06:37"
}, {
    "id": 45,
    "uuid": "7de3410f-82c2-4b30-8e69-56906b16da4b",
    "project_id": 8,
    "name": "Montageende",
    "due_date": "2019-12-16",
    "material_delivery": 0,
    "closed_at": null,
    "closed_from": null,
    "deleted_at": null,
    "created_at": "2019-12-30 16:43:04",
    "updated_at": "2019-12-30 16:43:04"
}, {
    "id": 46,
    "uuid": "8b034697-543c-46f6-a5be-104011700fb9",
    "project_id": 8,
    "name": "Lieferung",
    "due_date": "2020-01-25",
    "material_delivery": 1,
    "closed_at": null,
    "closed_from": null,
    "deleted_at": null,
    "created_at": "2019-12-30 16:43:04",
    "updated_at": "2019-12-31 13:57:16"
}, {
    "id": 47,
    "uuid": "ec1101cf-97cc-4eed-a2c6-0685b7cc073b",
    "project_id": 8,
    "name": "Abnahme",
    "due_date": "2020-03-05",
    "material_delivery": 0,
    "closed_at": null,
    "closed_from": null,
    "deleted_at": null,
    "created_at": "2019-12-30 16:43:04",
    "updated_at": "2019-12-30 16:43:04"
}, {
    "id": 48,
    "uuid": "deb7324a-64f2-4e1c-a358-87fdb95430ea",
    "project_id": 8,
    "name": "Rechnung",
    "due_date": "2020-04-14",
    "material_delivery": 0,
    "closed_at": null,
    "closed_from": null,
    "deleted_at": null,
    "created_at": "2019-12-30 16:43:04",
    "updated_at": "2019-12-30 16:43:04"
}]

I tried with filter but no success.

Finally, I like to know, if there are more than one item with the value 1 in material_delivery

Upvotes: 1

Views: 62

Answers (1)

T. Short
T. Short

Reputation: 3614

There are many ways to do this. You could use filter, like you initially tried:

methods: {
    countMaterialDelivery(array) {
        return array.filter(item => item.material_delivery === 1).length;
    }
}

Upvotes: 1

Related Questions