bflydesign
bflydesign

Reputation: 483

Add count as property to javascript object

I have two arrays:

Every data record belongs to a specific category.

With every use of a filter (e.g. searchfield) on the datarecords, I want to add the total count of records per category to each category-object, so I can display them on every tab-pane (e.g. text: Algemeen, value: 'general, countFiltered: 2)

I made a fiddle for this.

Till now I get no count-filtered, locally I get a constant error corresponding to line 78 in my fiddle:

Uncaught (in promise) Type Error: Cannot read property '0' of undefined

How can I achieve this?

Upvotes: 2

Views: 78

Answers (1)

Paul
Paul

Reputation: 141935

this isn't what you think it is here:

var filtered = results.filter(function (results) {
    return (results.category.toLowerCase() == this.categories[cat].value.toLowerCase());
});

You can either pass in a thisArg to filter:

var filtered = results.filter(function (results) {
    return (results.category.toLowerCase() == this.categories[cat].value.toLowerCase());
}, this);

or in ES6 you can use an arrow function:

var filtered = results.filter(
    results => results.category.toLowerCase() == this.categories[cat].value.toLowerCase() 
);

Upvotes: 7

Related Questions