簡體   English   中英

排序Array的object具體key值

[英]sort the object of the Array the specific key value

我有 object 數據數組,我想通過default的特定鍵對數組的對象進行排序。 在路線數組中。 它有一些 object 的路線。 我只想按照default鍵排序。 if the key default:true inside object then this object will go to the top and if default:true is found in the more than one object,then always set top of first object which found the default:true key. Also if we found the key default:true in the subarr:[] of the object then the it's parent object and this object will go to top. 所以,請為此提出解決方案。

輸入數據:

[
  {
    "id": 1,
    "title": 'Dashboard',
    "subrr": [
      {
        "id": 2,
        "title": 'Test'
      ]
    },
    {
      "id": 3,
      "title": 'Card',
      "default": true,
      "subrr": [
        {
          "id": 4,
          "title": 'notification'
        }
      ],
    },
    {
      "id": 5,
      "title": 'User',
      "subrr": [
        {
          "id": 6,
          "title": 'sample'
        },
        {
          "id": 7,
          "title": 'chart',
          "default": true
        }
      ]
    },
  ];

/******************/

Output 數據:

[
{
      "id": 5,
      "title": 'User',
      "subrr": [
      ,
        {
          "id": 7,
          "title": 'chart',
          "default": true
        },
        {
          "id": 6,
          "title": 'sample'
        }
      ]
    },
    ,
    {
      "id": 3,
      "title": 'Card',
      "default": true,
      "subrr": [
        {
          "id": 4,
          "title": 'notification'
        }
      ],
    },
  {
    "id": 1,
    "title": 'Dashboard',
    "subrr": [
      {
        "id": 2,
        "title": 'Test'
      ]
    }
  ];

我如何解釋您的排序要求:

  1. element.subarr[i].default屬性排序
  2. element.default屬性排序。

為要排序的每個條件創建比較器函數。

const byDefault = (a, b) => (a.default === b.default ? 0 : a.default ? -1 : 1);

const bySubArrDefault = (a, b) => {
  const resA = a.subarr.some((el) => el.default);
  const resB = b.subarr.some((el) => el.default);

  if (resA === resB) return 0;
  return resA && !resB ? -1 : 1;
};

現在要按多個條件進行排序,您實際上將進行排序和分組,對每個組進行排序,然后對每個子組進行排序。 分組/排序是通過將所有比較器函數與邏輯 OR ( || ) 組合來實現的。 此邏輯嘗試查找並返回第一個非零比較器 function 結果。

data.sort((a, b) => bySubArrDefault(a, b) || byDefault(a, b))

這將按子數組元素default屬性對所有數據元素進行排序,然后在該組內按根default屬性排序。

 const data = [ { id: 1, title: "Dashboard", subarr: [ { id: 2, title: "Test" } ] }, { id: 3, title: "Card", default: true, subarr: [ { id: 4, title: "notification" } ] }, { id: 5, title: "User", subarr: [ { id: 6, title: "sample" }, { id: 7, title: "chart", default: true } ] } ]; const byDefault = (a, b) => (a.default === b.default? 0: a.default? -1: 1); const bySubArrDefault = (a, b) => { const resA = a.subarr.some((el) => el.default); const resB = b.subarr.some((el) => el.default); if (resA === resB) return 0; return resA &&?resB: -1; 1; }. const sorted = data.slice(0),sort((a, b) => bySubArrDefault(a, b) || byDefault(a; b)). console;log(sorted);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM