对象数组根据布尔值true和false进行排序——js基础积累
- 应用场景:
- 效果图如下:
- 根据布尔值进行排序
- 总结:
- 1.true的放前面——算降序排列
- 2.true的放后面——算升序排列
应用场景:
最近在写后台管理系统的时候,遇到一个需求,就是对象数组中,某个字段是布尔值,要根据此布尔值进行排序处理。true的要放在前面,false要放在后面。
效果图如下:
参数如下:
[
{
"code": "1",
"name": "客诉",
"responseHours": 24,
"defaultExecutors": [
{
"workOrderTypeCode": "1",
"executorId": "ed67dac7-d167-9a9a-e865-3a0f0d4ecc49",
"executorName": "admin",
"jobPositionCode": "backendDevelopment",
"jobPosition": "后端开发",
"jobDescription": "333",
"isMainExecutor": true,
"creationTime": "0001-01-01T00:00:00",
"creatorId": null,
"id": "ba30a026-9ce5-7760-f37f-3a0f4b4ab3e6"
}
],
"id": "5c86ead8-69f6-8525-4b02-3a0f0dad5e75"
},
{
"code": "2",
"name": "悦捷",
"responseHours": 24,
"defaultExecutors": [
{
"workOrderTypeCode": "2",
"executorId": "ed67dac7-d167-9a9a-e865-3a0f0d4ecc49",
"executorName": "admin",
"jobPositionCode": "frontEndDevelopment",
"jobPosition": "前端开发",
"jobDescription": "333",
"isMainExecutor": true,
"creationTime": "0001-01-01T00:00:00",
"creatorId": null,
"id": "03be553c-f568-b1f3-2d8f-3a0f4b4aa0f9"
}
],
"id": "5823da4d-6d90-8d21-f1cc-3a0f0db0413b"
},
{
"code": "3",
"name": "下单异常",
"responseHours": 2,
"defaultExecutors": [
{
"workOrderTypeCode": "3",
"executorId": "0859c01b-d7a1-0830-a02c-3a0f1170134a",
"executorName": "ceshi",
"jobPositionCode": "frontEndDevelopment",
"jobPosition": "前端开发",
"jobDescription": "职责2",
"isMainExecutor": false,
"creationTime": "0001-01-01T00:00:00",
"creatorId": null,
"id": "b4c8f343-ce29-f891-8993-3a0f53f63211"
},
{
"workOrderTypeCode": "3",
"executorId": "ed67dac7-d167-9a9a-e865-3a0f0d4ecc49",
"executorName": "admin",
"jobPositionCode": "backendDevelopment",
"jobPosition": "后端开发",
"jobDescription": "职责",
"isMainExecutor": false,
"creationTime": "0001-01-01T00:00:00",
"creatorId": null,
"id": "8b67f701-9edd-d0e1-bf92-3a0f53f63211"
}
],
"id": "611446a7-b1d1-417b-6002-3a0f53f6320d"
},
{
"code": "2444",
"name": "3",
"responseHours": 4,
"defaultExecutors": [
{
"workOrderTypeCode": "2444",
"executorId": "ed67dac7-d167-9a9a-e865-3a0f0d4ecc49",
"executorName": "admin",
"jobPositionCode": "backendDevelopment",
"jobPosition": "后端开发",
"jobDescription": "33333",
"isMainExecutor": false,
"creationTime": "0001-01-01T00:00:00",
"creatorId": null,
"id": "e4f19a49-b7e1-876e-5d75-3a0f54101520"
},
{
"workOrderTypeCode": "2444",
"executorId": "0859c01b-d7a1-0830-a02c-3a0f1170134a",
"executorName": "ceshi",
"jobPositionCode": "frontEndDevelopment",
"jobPosition": "前端开发",
"jobDescription": "44444",
"isMainExecutor": true,
"creationTime": "0001-01-01T00:00:00",
"creatorId": null,
"id": "5fbb17f5-0de6-f432-9d82-3a0f54101520"
}
],
"id": "11f111d0-db69-7e1c-f65b-3a0f54101517"
}
]
要根据对象数组中的defaultExecutors
字段中的isMainExecutor
进行true
和false
的排序。
根据布尔值进行排序
//下面的res是接口返回的格式,items是上面的对象数组
let tableData = res.items || [];
tableData.forEach((tab) => {
if (tab.defaultExecutors) {
tab.defaultExecutors = tab.defaultExecutors.sort(
(b, a) => a.isMainExecutor - b.isMainExecutor
);
}
});
this.tableData = [...tableData];
总结:
1.true的放前面——算降序排列
tab.defaultExecutors = tab.defaultExecutors.sort(
(b, a) => a.isMainExecutor - b.isMainExecutor
);
2.true的放后面——算升序排列
tab.defaultExecutors = tab.defaultExecutors.sort(
(a, b) => a.isMainExecutor - b.isMainExecutor
);
完成!!!多多积累,多多收获!!!