I'd like to solve the following problem: I would like to filter an array by certain conditions with Vue.js and lodash (or pure JavaScript).
My objects
- I got a list of
Items. Each item contains (optional)categories. - The object
Carcan have acategory.
Array of Objects Items
"data": [
{
"id": 1,
"title": "Item 1",
"categories": [
{
"id": 4,
"title": "category 4",
"description": "",
},
{
"id": 5,
"title": "category 5",
"description": "",
}
]
},
{
"id": 2,
"title": "Item 2",
"categories": []
},
{
"id": 3,
"title": "Item 3",
"categories": []
}
]
Object Car
{
"category": {
"id": 5,
"title": "category 5",
"description": ""
},
"title": "Test"
}
My goal
I would like to only show those Items, which are passing at least one of these rules:
- The
Itemhas at least onecategorythat matches thecategoryof theCar - The
Itemhas nocategoryset
My approach
I am using a computed value to filter this.items:
computed: {
filteredItems: function () {
let vm = this;
return _.filter (this.items, function (item) {
return _.includes (item.categories, vm.car.category );
});
}
},
Current result:
filteredItems is always empty.