객체 배열은 부울 값 true 및 false에 따라 정렬됩니다. - js 기본 누적

애플리케이션 시나리오:

최근 백엔드 관리 시스템을 작성할 때 요구 사항에 직면했습니다. 즉, 개체 배열에서 특정 필드에 부울 값이 있고 이 부울 값을 기준으로 정렬해야 한다는 것입니다. 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에 따라 수행되어야 합니다. a i= 4> 정렬 중입니다. truefalse

부울 값으로 정렬

//下面的res是接口返回的格式,items是上面的对象数组
let tableData = res.items || [];
tableData.forEach((tab) => {
    
    
  if (tab.defaultExecutors) {
    
    
    tab.defaultExecutors = tab.defaultExecutors.sort(
      (a, b) => b.isMainExecutor - a.isMainExecutor
    );
  }
});
this.tableData = [...tableData];

요약하다:

1. 실제 항목을 먼저 배치 - 내림차순으로 계산

tab.defaultExecutors = tab.defaultExecutors.sort(
  (a, b) => b.isMainExecutor - a.isMainExecutor
);

2. 실제 내용을 뒤쪽에 배치 - 오름차순으로 정렬

tab.defaultExecutors = tab.defaultExecutors.sort(
  (a, b) => a.isMainExecutor - b.isMainExecutor
);

마치다! ! ! 많이 축적하고 많이 얻으세요! ! !

추천

출처blog.csdn.net/yehaocheng520/article/details/134848070