按键值对 JavaScript 数组中的对象进行排序
我可能想出了一个令人难以置信的粗略方法来完成这类事情,但我想我应该询问在场的许多专家。基本上,我有一个如下所示的数组:
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
},
];
基本上,该数组包含几个“错误”,每个错误都有 id、标题、摘要、状态、项目、区域和小时属性。这些错误中的每一个都将显示在我的 Web 应用程序上,但我允许用户选择它们的分组方式;按状态、项目或区域。根据他们从上面的选择框中选择的三个错误中的哪一个,我希望能够对所有错误进行排序,并按他们选择的类别对它们进行分组。然后,在显示它们时,为该类别的每个当前选项设置一个简单的标题。例如,如果他们要按状态排序,则类似于:
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"
我是否应该迭代整个错误数组,并根据要排序的类别,只需为该类别的每个可能选项创建一个新数组并添加给他们适当的错误?那么在上面的例子中,创建新数组 var activeBugs = []
和 var returnedBugs = []
吗?如果是这样,我的问题就是知道有哪些可能的选择。然后,在创建这些新数组之前,我是否应该首先迭代整个错误数组,以查看所需组类别存在哪些可能的选项?
不诉诸其他 jQuery 插件的最佳方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了详细说明我质疑是否需要排序/过滤的评论,您可以在显示逻辑中做一些(非常粗略的)类似的事情吗?
工作演示: http://jsfiddle.net/TTApQ/1/
这样就没有排序或过滤根本不需要,而且您只需要对数组进行一次传递即可。
To elaborate on my comment questioning the need to sort/filter at all, can you do something (very roughly) like this in your display logic?
Working demo: http://jsfiddle.net/TTApQ/1/
That way there's no sorting or filtering at all, and you only need to make one pass over the array.
用户会得到所有错误的副本吗?或者您只发送前 100 个?
Will the user have a copy off all the bugs? or do you just send, lets say, the first 100?
使用 array.sort 按状态对数组进行排序。迭代数组,记住上一次迭代的 .status 属性是什么。当它发生变化时,创建一个新的视觉表示。
警告:最终您会希望让数据库完成所有排序/分组/分页等操作。
Sort the array by status using array.sort. Iterate through the array remembering what the previous iteration's .status property was. When it changes, create a new visual representation.
Warning: Eventually you'll wish you had let the database do all the sorting/grouping/paging/etc.
这是一个使用
.filter()
的示例,正如我在评论中所述:Here's an example using
.filter()
as I described in my comment: