0

I've got an array. If someone reserve a table, reserve in the array is set to true.

$rootScope.tafels = [
    {id: 0, text:'table 2a, 4 persons.', reserve:false}, 
    {id: 1, text:'table 3b, 8 persons.', reserve:false}
];

And I've got an function for returning the length of the array:

$rootScope.getTotaalTafels = function()
    { return $rootScope.tafels.length; };

Now the difficult part that I can not solve, maybe you can:

I want to return the total tables that are not reserved, with my function showed above. How do I apply a filter to it?

2 Answers 2

2

Javascript 1.6 implements the filter function which allows exactly this:

$rootScope.getTotaalTafels = function(){
    return $rootScope.tafels.filter(function(value,index){
        return !value.reserve;
    }).length;
};

If you need to support older browsers there is a backward compatible function implementing this behaviour available here.

Sign up to request clarification or add additional context in comments.

Comments

2

Use $filter with the last version of AngularJS ;)

1 Comment

Can you provide an example of how $filter would be used for the dataset in question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.