0

I'm binding JSON objects to a list, but I only want to show one (the first, since the results are ordered) item per user. The JSON I'm getting back is per item, with a user object as a property (item.user.username, etc.). With jQuery I'd do something like:

var arr = ... JSON objects ...
var seen_users = [];
var items = [];
$.each(arr, function(i, item){
    if (!$.inArray(item.user.id, arr) === -1){
        items.push(item);
        seen_users.push(item.user.id);
    }
}

But is there a more Angular-thonic way to do this? I've been looking at filters but can't figure out an easy way (other than iterating through the bound data like above) to do this.

UPDATE:

AngularJS code is a little much to post, but basically I have a $scope.items array of JSON objects in my controller that I get via an API courtesy of $http and an ItemFactory, and pretty basic HTML to display things:

<ul id="items">
    <li class="item" data-ng-repeat="item in items">
       {{item.title}} | {{item.posted}}
    </li>
</ul>
2
  • 2
    Can I see your angular code? also the html that will display this? Commented Dec 11, 2013 at 21:54
  • ideally put code into jsfiddle.net or plunker with sample data...also post here though Commented Dec 11, 2013 at 22:02

2 Answers 2

1

You can create a custom filter like this

app.filter('myFilter', [function () {
    return function (arr) {
        var seen_users = [];
        var items = [];
        $.each(arr, function (i, item) {
            if (!$.inArray(item.user.id, arr) === -1) {
                items.push(item);
                seen_users.push(item.user.id);
            }
        });
        return seen_users;
    };
}]);

And use it in the template like this

<li class="item" data-ng-repeat="item in (items | myFilter)">
Sign up to request clarification or add additional context in comments.

1 Comment

This is still iterating through every item (again). Since the ng-repeat is already iterating through the items, I'm looking for a way to only iterate once for efficiency. The filter idea should be able to get me there, though, maybe by just checking each item as it's passed to the filter (not sure if the filter is applied once to the array or once to each item).
0

In your html you can index your array like this: UPDATED:

<ul>
    <li>
      {{item[0].title}} | {{item[0].posted}}
    </li>
</ul>

and you script should be like:

$scope.items = []; // with your data in it.

There is a couple of other way to do this. If you want it to be dynamic (e.g. If you want to display more items on demand later:

<span data-ng-repeat="item in displayItems">
 {{item.user.id}}
</span>

The script for that would be like:

$scope.items = []; // this is your original array with all the data in it.
$scope.displayItems = []; // this is an empty array that a function will fill up with        data
myFunction = function(){
   // put data selection logic here
   $scope.displayItems.push($scope.items[i]);
};

2 Comments

That's basically what I'm doing now with the jQuery posted above - was just hoping this was a solved problem in angular that wouldn't require me iterating through the entire array since angularjs makes it really easy to consume JSON data as it is.
Then you can just indexing that array in your html, like I posted above. {{items[0].something}} will work just fine.

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.