I may have come up with an incredibly crude way to get this sort of thing accomplished, but I figured that I'd ask the many experts present here at SO. Basically, I have an array that looks something like the following:
var bugs = [
{
id: "197526",
title: "Updating Lighthouse",
summary: "Enhancing the UI of Lighthouse",
status: "Active",
project: "Lighthouse",
area: "Internal Web Applications",
hours: 19
},
{
id: "190328",
title: "Adding Login Authentication to Lighthouse",
summary: "Create a login authentication process for Lighthouse",
status: "Active",
project: "Lighthouse",
area: "Administration",
hours: 12
},
...
{
id: "187562",
title: "Create a Maintenance Page",
summary: "Create a maintenance page to be displayed with the company site is down",
status: "Resolved",
project: "Other",
area: "Internal Web Projects",
hours: 4
},
];
Basically, the array holds several "bugs," each with an id, title, summary, status, project, area, and hours property. Each of these bugs are going to be displayed on my web application, but I allow the user to select how they will be grouped; either by status, project, or area. Depending upon which of the three they select from a select box above, I want to be able to sort through all of the bugs and group them by whichever category they chose. Then, when it comes to displaying them, have a simple header for each present option for that category. For example, if they were to sort by status, it would be something like:
Group By: Status
Active
------
Bug with status: "active"
Bug with status: "active"
Bug with status: "active"
Resolved
--------
Bug with status: "resolved"
Bug with status: "resolved"
Should I iterate through the entire array of bugs and, based on the category to sort by, simply create a new array for each possible option of that category and add the appropriate bugs to them? So in the case above, create new arrays var activeBugs = []
and var resolvedBugs = []
? If so, my problem would then be knowing what possible options there are. Should I then first iterate through the entire bugs array to see what possible options are present for the desire group category before creating these new arrays?
What's the best way to do this without resorting to other jQuery plugins?
bugs
array that I want grouped for displaying. The servlet itself accesses a database in order to get the bug information to begin with. So I suppose I can pass thegroupBy
option during the AJAX call and add it to theGROUP BY
part of my SQL statement... – Kris Schouw Nov 19 '11 at 0:18.sort()
, you're looking for.filter()
. Unfortunately the IE support is not good (9 only), so you may or may not want to use it depending on the planned use. Edit: Forgot to include a link - if you're interested in having the IE work-around, you can find the proper code at MDN – Alec Ananian Nov 19 '11 at 0:20