Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to group the elements in an array that have the same id using angular js Filter

I have this filter:

.filter('groupBySectionNum', function(){
    return function(items){
        if(items){
            var finalItems = [];
            for (var i = 0; i < items.length; i++) {
                var _id = items[i]._id;
                if(!finalItems[_id]){
                    finalItems[_id]=[];
                }
                finalItems[_id].push(items[i]);

            }
            return finalItems;
        }
    }
})

which uses the is as an index so that it groups objects with similar id

the results wont show up in the html

<a ng-repeat="a in (filteredSecs = cal|groupBySectionNum)">
    <accordion-group heading="try">
        something {{a[0]._id}}
    </accordion-group>
</a>

how can i properly group the items? or properly read the filtered array?

share|improve this question
    
finalItems should not be an array, it should just be an object. Arrays should have contiguous indices (even though JavaScript allows what you're doing). –  jelinson Mar 2 at 8:19
    
Can you create a JSFiddle that demonstrates the issue? –  jelinson Mar 2 at 8:19
    
I could not figure it out. I have altered some code logic in order to achieve the same results, without filtering the whole array... thank you –  Salam0smy Mar 5 at 15:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.