簡體   English   中英

如何根據javascript中的多個字段對數組進行排序

[英]how to sort an array based on multiple fields in javascript

我有一個數組對象

events = [
   {
     year: "2019",
     month: "June",
     title: "title",
     desc: "desc"
   },
   {
     year: "2019",
     month: "June",
     title: "title",
     desc: "desc"
    },
    {
      year: "2019",
      month: "July",
      title: "title",
      desc: "desc"
    },
    {
      year: "2018",
      month: "June",
      title: "title",
      desc: "desc"
     },
     {
       year: "2018",
       month: "March",
       title: "title",
       desc: "desc"
     },
     {
       year: "2018",
       month: "March",
       title: "title",
       desc: "desc"
      }
    ]

如何對數組進行排序以顯示相似的年份和月份,我需要在反應組件中使用它,例如一行將顯示 2019 年 6 月和 2019 年 7 月,然后是 2018 年 6 月,依此類推,

任何幫助,將不勝感激

您需要為.sort提供自定義比較器函數以使用月份和年份進行排序。

 const events = [ { year: "2019", month: "June", title: "title", desc: "desc" }, { year: "2019", month: "June", title: "title", desc: "desc" }, { year: "2019", month: "July", title: "title", desc: "desc" }, { year: "2018", month: "June", title: "title", desc: "desc" }, { year: "2018", month: "March", title: "title", desc: "desc" }, { year: "2018", month: "March", title: "title", desc: "desc" } ]; // Custom comparator function function sortByMonthYear(a, b) { const keyA = `${a.month} ${a.year}`; const keyB = `${b.month} ${b.year}`; if (keyA.localeCompare(keyB)) { return -1; } else if (keyA === keyB) { return 0; } return 1; } const grouping = events.sort(sortByMonthYear); console.log(grouping);


更新:

您還可以使用Array#prototype#reduce對數據進行分組

 const events = [{ year: "2019", month: "June", title: "title", desc: "desc" }, { year: "2019", month: "June", title: "title", desc: "desc" }, { year: "2019", month: "July", title: "title", desc: "desc" }, { year: "2018", month: "June", title: "title", desc: "desc" }, { year: "2018", month: "March", title: "title", desc: "desc" }, { year: "2018", month: "March", title: "title", desc: "desc" } ]; const grouping = events.reduce((acc, curr) => { const key = `${curr.month} ${curr.year}`; if (!acc[key]) { acc[key] = [curr]; } else { acc[key].push(curr); } return acc; }, {}); console.log(grouping);

通過它們創建對一年中所有月份和flatMap的引用,在 flatMap 中對原始事件運行過濾器以僅返回與迭代中的當前月份共享同一month任何事件。 完成此操作后,您可以對列表進行sort ,以確保事件按year排序(從小到大)

 const MOY = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] const events = [ {year: "2019",month: "June",title: "title",desc: "desc"}, {year: "2019",month: "June",title: "title",desc: "desc"}, {year: "2019",month: "July",title: "title",desc: "desc"}, {year: "2018",month: "June",title: "title",desc: "desc"}, {year: "2018",month: "March",title: "title",desc: "desc"}, {year: "2018",month: "March",title: "title",desc: "desc"} ] const sortedByMonth = MOY.flatMap(m => events.filter(({month}) => m === month)) const sortedByYear = sortedByMonth.sort((eA, eB) => eA.year < eB.year) console.log(sortedByMonth) console.log(sortedByYear)

或者,如果您想按月對事件進行分組但按年排序,則執行

 const MOY = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] const events = [ {year: "2019",month: "June",title: "title",desc: "desc"}, {year: "2019",month: "June",title: "title",desc: "desc"}, {year: "2019",month: "July",title: "title",desc: "desc"}, {year: "2018",month: "June",title: "title",desc: "desc"}, {year: "2018",month: "March",title: "title",desc: "desc"}, {year: "2018",month: "March",title: "title",desc: "desc"} ] const sorted = MOY.flatMap(m => ( events .filter(({month}) => m === month) .sort((eA, eB) => eA.year < eB.year) )) console.log(sorted)

暫無
暫無

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

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