Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Object limitTo filter not native ot AngularJS? #6300

Closed
RavenHursT opened this issue Feb 17, 2014 · 2 comments
Closed

Object limitTo filter not native ot AngularJS? #6300

RavenHursT opened this issue Feb 17, 2014 · 2 comments

Comments

@RavenHursT
Copy link

Why isn't something like this native to AngularJS? Seems rather simple to implement...

Filter:

.filter('objLimitTo', [function(){
        return function(obj, limit){
            var keys = Object.keys(obj);
            if(keys.length < 1){
                return [];
            }

            var ret = new Object,
                count = 0;

            angular.forEach(keys, function(key, arrayIndex){
                console.log(key);
                if(count >= limit){
                    return false;
                }
                ret[key] = obj[key];
                count++;
            });

            return ret;
        };
    }])

Markup:

<ul>
<li ng-repeat="(k, v) in items | objLimitTo:4">{{ v}} </l>
</li>
@pkozlowski-opensource
Copy link
Member

I guess there are plenty of useful filters that are not part of the core as the framework can't come with all the possible utilities. This is why we've got great community that creates awesome stuff and advertises it on http://ngmodules.org/

Regarding a filter you are bringing up here, I would say that the main reason is that keys enumeration order is not guaranteed (and is different) across different browsers. So you would get different behaviour of the same code + data in different browsers and this would be unpleasant surprise...

@RavenHursT
Copy link
Author

Ah.. thanks for the explaination @pkozlowski-opensource

Closing..

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants