Passing index of item to Array filters.

1,467 views
Skip to first unread message

Tyler Breisch

unread,
Nov 14, 2011, 12:10:09 PM11/14/11
to AngularJS
Hey all,

I was attempting to write a filter in a repeater, and was surprised to
only find the value of the item passed into the filter function. Is
there any reason why the index of the item is not passed in as well?
I went ahead and made my own version of angular.Array.filter to pass
in the index of the item in the collection to all predicate functions.


Changes are below:
"angular.Array.filter"

predicates.check = function(value, index) {
for (var j = 0; j < predicates.length; j++) {
if (!predicates[j](value, index)) {
return false;
}
}
return true;
};

.......

for (var j = 0; j < array.length; j++) {
var value = array[j];
if (predicates.check(value, j)) {
filtered.push(value);
}
}


Thoughts?


Vojta Jina

unread,
Nov 14, 2011, 4:37:13 PM11/14/11
to ang...@googlegroups.com
This sounds like a good thing. Here is an issue for it: https://github.com/angular/angular.js/issues/654

V.

Vojta Jina

unread,
Nov 15, 2011, 1:27:41 PM11/15/11
to ang...@googlegroups.com
Hi Tyler,

we've just discussed this with Misko and Igor and we can't find any good use case for that... Do you have some ?

I can only think of filtering all even/odd items, which is probably not really useful...

Thanks,

V.

Tyler Breisch

unread,
Nov 15, 2011, 2:14:29 PM11/15/11
to AngularJS
Sure.

My current requirement is to only show a current range of items in a
collection, with the range changing dynamically.

From template
dataArray.$filterBetter(list.carousel)

JS:
data.carousel = function(item, index) {
var start = this.displayIndex;
return index >= start && index < start + self.displayCount;
}

Something like this.

Misko Hevery

unread,
Nov 15, 2011, 2:28:50 PM11/15/11
to ang...@googlegroups.com
So to me it sounds like mixing concerns. Don't you want something like this?

dataArray.$filter(someFn).$limitTo(...)

The to are different concerns and should be treated as such, no?


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.


Tyler Breisch

unread,
Nov 15, 2011, 2:38:21 PM11/15/11
to ang...@googlegroups.com
What would be passed into limitTo?

Also, does it not make sense to follow what other javascript standards are doing?


callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.

Misko Hevery

unread,
Nov 15, 2011, 2:41:12 PM11/15/11
to ang...@googlegroups.com
Perhaps you should write your own limitTo which can select the relevant set. It seams strange to me that the filter should be responsible for selecting a subset of items based on the internal location of the item. Just my $0.02

- misko

--

Tyler Breisch

unread,
Nov 15, 2011, 3:44:07 PM11/15/11
to ang...@googlegroups.com
Ahh sorry I didn't see that limitTo was an angular defined function already. 

However how would you select items in the middle of an array?

My use case is having a large set of data, and wanting to write essentially a carousel around that data.

My presumption was to use a repeater with a filter to choose which page of data to display.  When the page number changed the filter on the repeater would select the next group of data.

Does this make sense? 

Tyler Breisch

unread,
Nov 15, 2011, 4:03:04 PM11/15/11
to ang...@googlegroups.com
Actually I'm rethinking my implementation.

This seems to be the better approach.

ng:repeat="item in getCarouselItems()

Then in the controller I can define a function which will return the proper data set.

Thanks for helping me get some clarity.


Misko Hevery

unread,
Nov 15, 2011, 4:11:24 PM11/15/11
to ang...@googlegroups.com
:-), that sounds right.

Reply all
Reply to author
Forward
0 new messages