When creating a filter in angularjs how do I access the previous item in the array? i.e I want to compare the current element to the previous one
Asked
Active
Viewed 361 times
0
-
Show us your code please. – m90 Apr 18 '13 at 17:33
1 Answers
1
Hard to figure out exactly what you're trying to achieve, but I think if you're using an ng-repeat you can do this:
HTML:
<div data-ng-repeat="element in elements">
<div>{{ element | myFilter:elements:$index</div>
</div>
Javascript
.filter("myFilter", function(){
return function(input, elements, index){
if(index > 0){
var previousElement = elements[index - 1]
}
return input;
}
});
Edit
Ok now that you've specified what you're trying to do, you can use a filter in a different way to instead remove the duplicate dates.
Here's the filter:
.filter("uniqueDates", function(){
return function(input){
var returnInput = [];
for(var x = 0; x < input.length; x++){
if(returnInput.indexOf(input[x]) === -1){
returnInput.push(input[x]);
}
}
return returnInput;
};
})
Here's a jsfiddle with the html and everything all wired up that you can extrapolate from: http://jsfiddle.net/nE9EE/

Mathew Berg
- 28,625
- 11
- 69
- 90
-
using ng-repeat, I have an array of dates and only want to show each unique date, not every date – raphael_turtle Apr 18 '13 at 17:42
-
Seems like you filter just needs to [remove duplicate entries from the array](http://stackoverflow.com/a/9229821/586621). – Jay Apr 18 '13 at 17:47
-