我有一个对象的数组,我需要排序在JavaScript(es6很好),或jQuery。数据比常规的对象数组复杂一点,因为该数据位于子对象中。我需要为要排序的数据位于动态键的对象中的动态键进行排序。例如,我需要对'id'进行升序或降序排序,数据位于id.data中。Javascript排序的对象的子对象的对象数组
[{
"_row": {},
"_parent": {},
"id": {"data": 112, "cell": {}},
"name": {"data": "D'Amore, Volkman and Cole", "cell": {}},
"check_name": {"data": "", "cell": {}},
"account_number": {"data": "5534867831801846", "cell": {}},
"main_email": {"data": "[email protected]", "cell": {}},
"cc_email": {"data": "[email protected]", "cell": {}},
"main_phone": {"data": "1-845-550-6422", "cell": {}},
"work_phone": {"data": "+1 (859) 399-6372", "cell": {}},
"mobile": {"data": "292-242-7626 x798", "cell": {}},
"fax": {"data": "", "cell": {}},
"active": {"data": 1, "cell": {}},
"billing_address": {"data": "24226 Mackenzie Junctions Suite 393\nDonaldside, GA 87531", "cell": {}},
"shipping_address": {"data": "478 Toy Loaf Suite 552\nWaelchiberg, ND 70701-3633", "cell": {}},
"comments": {"data": "", "cell": {}}
}, {
"_row": {},
"_parent": {},
"id": {"data": 120, "cell": {}},
"name": {"data": "Carroll, Rice and Reilly", "cell": {}},
"check_name": {"data": "", "cell": {}},
"account_number": {"data": "4539358256447", "cell": {}},
"main_email": {"data": "[email protected]", "cell": {}},
"cc_email": {"data": "[email protected]", "cell": {}},
"main_phone": {"data": "612-864-9512", "cell": {}},
"work_phone": {"data": "(519) 761-1805", "cell": {}},
"mobile": {"data": "+1-730-669-4339", "cell": {}},
"fax": {"data": "", "cell": {}},
"active": {"data": 1, "cell": {}},
"billing_address": {"data": "848 Rashawn Causeway\nHauckberg, GA 21193", "cell": {}},
"shipping_address": {"data": "3458 Wolff Cape Suite 336\nWolfmouth, DC 35821", "cell": {}},
"comments": {"data": "", "cell": {}}
}, {
"_row": {},
"_parent": {},
"id": {"data": 122, "cell": {}},
"name": {"data": "Denesik and Sons", "cell": {}},
"check_name": {"data": "", "cell": {}},
"account_number": {"data": "6011079688853496", "cell": {}},
"main_email": {"data": "[email protected]", "cell": {}},
"cc_email": {"data": "[email protected]", "cell": {}},
"main_phone": {"data": "569-382-2580 x1764", "cell": {}},
"work_phone": {"data": "705.782.2219", "cell": {}},
"mobile": {"data": "936-586-1978", "cell": {}},
"fax": {"data": "", "cell": {}},
"active": {"data": 1, "cell": {}},
"billing_address": {"data": "1864 Donnelly Parkway Suite 222\nPort Hailieburgh, NC 08808-0938", "cell": {}},
"shipping_address": {"data": "28476 Jerald Valleys Apt. 537\nNorth Vancemouth, DC 16865-0793", "cell": {}},
"comments": {"data": "", "cell": {}}
}, {
"_row": {},
"_parent": {},
"id": {"data": 124, "cell": {}},
"name": {"data": "Trantow, Botsford and Runolfsson", "cell": {}},
"check_name": {"data": "", "cell": {}},
"account_number": {"data": "4556163511909216", "cell": {}},
"main_email": {"data": "[email protected]", "cell": {}},
"cc_email": {"data": "[email protected]", "cell": {}},
"main_phone": {"data": "+16989316200", "cell": {}},
"work_phone": {"data": "969.610.8041 x8593", "cell": {}},
"mobile": {"data": "680.717.5368", "cell": {}},
"fax": {"data": "", "cell": {}},
"active": {"data": 1, "cell": {}},
"billing_address": {"data": "96778 VonRueden Square Suite 421\nKennafort, SC 70938", "cell": {}},
"shipping_address": {"data": "13334 Orion Green\nEast Lillieborough, ND 19714", "cell": {}},
"comments": {"data": "", "cell": {}}
}]
并发症的下一步是,我想通过ID进行排序,然后另一个关键,如主动,然后命名等
什么想法?我可以驾驭.sort
吗? (这看起来像我不使用细胞对象,也许我可以删除它,使我的生活更轻松,我想我在几年前添加了一个非常重要的原因)
这里是我所想的out ..第一种排序工作正常,排序多个列似乎没有工作。
尝试1:
//sort_array looks like [{db_field:'asc'},etc...]
//we need to sort row based on data for this.tdo[row][db_filed]['data']
this.tdo.sort((a,b) => {
sort_array.forEach(sort => {
let keys = Object.keys(sort);
let name = keys[0];
if(sort[keys[0]] =='asc')
{
this.tdo = this.tdo.sort(this.dynamicSort(name));
}
else
{
this.tdo = this.tdo.sort(this.dynamicSort('-'+name));
}
})
})
其中dynamicSort
从堆栈溢出Sort array of objects by string property value in JavaScript
dynamicSort(property) {
let sortOrder = 1;
if (property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a, b) {
let result = (a[property].data < b[property].data) ? -1 : (a[property].data > b[property].data) ? 1 : 0;
return result * sortOrder;
}
}
尝试2拉动,使用thenBy这是相当凉爽。我发现了一个例子,如何堆叠在其关闭的问题排序:
let sort_stack = firstBy(function (v1, v2) { return 0 });
sort_array.forEach(sort => {
let keys = Object.keys(sort);
let name = keys[0];
if(sort[keys[0]] =='asc')
{
sort_stack = sort_stack.thenBy(function (v1) { return v1[name].data; });
}
else
{
sort_stack = sort_stack.thenBy(function (v1) { return v1[name].data ; },-1);
}
})
this.tdo.sort(sort_stack);
此外,我可能需要限制用户输入的子排序依据的数据类型的...像id
将整理一次
不知道为什么我得到了下来票,这种是相当复杂,超出了.sort
文档
是的,你可以使用['.sort'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/分类)。简单地写一个比较函数,完全符合你描述的内容。 – Hamms
它有什么复杂的?不是比较'a.id'到'b.id',而是比较'a.id.data'和'b.id.data'。 –
http://www.javascriptkit.com/javatutors/arraysort2.shtml – CodingYoshi