I want to filter items from the list. When a certain category will be provided as a value to the target variable, the filter method should return only items with that category. but when no category is provided I want to get items of all categories regardless of category. In my code below works ok, but I want more clear logic to filter items when no category is provided as a target.
let items = [
{'name':"A",'category':"a"},
{'name':"B",'category':"b"},
{'name':"C",'category':"c"}
]
let target;
console.log(items.filter( item => {
if(target==undefined){
return true;
}
return (item['category']===target);
}));