One array of objects that I have on hand looks like this:
var arr = [{id:1, name: 'frank', type: 'great'}, {id:2, name: 'john', type: 'good'}, {id:3, name: 'mark', type: 'great'}, {id:4, name: 'mary', type: 'bad'}];
Sometimes, I find myself needing to display 3 different html elements. Each element should contain a list of names with the same "type".
I could run 3 separate loops to search for these names, but I'm wondering if there's a more efficient method. The best approach I've thought of is to "filter" the array for each iteration. Even though I would still loop 3 times, it might be a more targeted way to go through only the elements that matter.
I'm unsure about how to go about filtering this data. I'm using angular.js currently, but I'm open to using plain JavaScript as well. Is there a better solution out there?
*****EDIT*****
Special thanks to duffy for suggesting the filter function. Here's my dilemma:
Within an .html file, there's a div structured like this:
<div ng-repeat="person in peopleContainer.get_people()">
The peopleContainer is defined as a factory in services.js. This factory holds the array of individuals and functions for adding new people or retrieving the entire list.
Currently, the ng-repeat directive above doesn't seem to be functioning properly. When a new person is added, it reflects in the array, but no changes are seen in the div container. The ng-repeat isn't triggering at all. Could this issue be due to the location of the people array within the service.js file? If so, is there a workaround for this situation?