亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在具有 2 個日期的腳本中對 json 數據進行排序

在具有 2 個日期的腳本中對 json 數據進行排序

米琪卡哇伊 2022-09-23 21:33:16
我有以下 json 數據:data = [  {    stateCode: 'CH',    startDate: new Date('06/12/2020 08:00'),    startDateWithDelay: new Date('06/12/2020 08:00'),  },  {    stateCode: 'LA',    startDate: new Date('06/12/2020 08:00'),    startDateWithDelay: new Date('06/12/2020 08:30'),  },  {    stateCode: 'NY',    startDate: new Date('06/12/2020 06:00'),    startDateWithDelay: new Date('06/12/2020 06:00')  }]默認情況下,數據應按起始日期排序。但是,如果 2 條記錄的開始日期相同,則應按狀態日期與Delay 屬性進行排序。在上面的示例中,前 2 條記錄的開始日期是相同的。因此,在這種情況下,應根據“開始日期”屬性對其進行排序。排序后,結果應根據 2 個日期顯示以下狀態:紐約 紐約 芝加哥CH 洛杉磯 洛杉磯我正在使用以下代碼根據開始日期進行排序import { sortBy } from 'lodash-es'data = data.sortBy(data, (obj) => obj.startDate);當前 2 條記錄的啟動日期相同時,如何按“開始日期”屬性完成排序。
查看完整描述

4 回答

?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

您可以使用傳遞給 的自定義排序函數基于兩個參數進行排序Array.prototype.sort


const data = [

  {

    stateCode: 'CH',

    startDate: new Date('06/12/2020 08:00'),

    startDateWithDelay: new Date('06/12/2020 08:00'),

  },

  {

    stateCode: 'LA',

    startDate: new Date('06/12/2020 08:00'),

    startDateWithDelay: new Date('06/12/2020 08:30'),

  },

  {

    stateCode: 'NY',

    startDate: new Date('06/12/2020 06:00'),

    startDateWithDelay: new Date('06/12/2020 06:00')

  }

]


const sorted = data.sort((a, b) => {

  const startDateComparison = a.startDate - b.startDate

  if (startDateComparison !== 0) return startDateComparison

  return a.startDateWithDelay - b.startDateWithDelay

})


console.log(sorted)


查看完整回答
反對 回復 2022-09-23
?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

最好的方法是使用 as, 函數,使我們能夠靈活地覆蓋任何類型的數組數據的數組的排序行為,并且可以用于自定義屬性的自定義排序順序??纯次遗e的最后一個例子。Array.prototype.sortArray.prototype.sort


const data = [

  {

    stateCode: "CH",

    startDate: new Date("06/12/2020 08:00"),

    startDateWithDelay: new Date("06/12/2020 08:00"),

  },

  {

    stateCode: "LA",

    startDate: new Date("06/12/2020 08:00"),

    startDateWithDelay: new Date("06/12/2020 08:30"),

  },

  {

    stateCode: "NY",

    startDate: new Date("06/12/2020 06:00"),

    startDateWithDelay: new Date("06/12/2020 06:00"),

  },

];


const sorted = data.sort((a, b) => {

  // one liner using conditional operator

  return a.startDate - b.startDate === 0

    ? a.startDateWithDelay - b.startDateWithDelay

    : a.startDate - b.startDate;

});


console.log(sorted);

在同一示例中,如果我們必須按“LA”,“NY”和“CH”的順序對狀態進行排序,那么我們也可以這樣做,如下面的示例所示。


const data = [

  {

    stateCode: "CH",

    startDate: new Date("06/12/2020 08:00"),

    startDateWithDelay: new Date("06/12/2020 08:00"),

  },

  {

    stateCode: "LA",

    startDate: new Date("06/12/2020 08:00"),

    startDateWithDelay: new Date("06/12/2020 08:30"),

  },

  {

    stateCode: "NY",

    startDate: new Date("06/12/2020 06:00"),

    startDateWithDelay: new Date("06/12/2020 06:00"),

  },

];


// Custom Sorting based on States in order of 'LA', 'NY' & 'CH'

const sorted = data.sort((a, b) => {

  const sCa = a.stateCode; //state code of a

  const sCb = b.stateCode; //state code of b

  return (sCa === 'LA' && sCb !== 'LA') || (sCa === 'NY' && sCb === 'CH') ? -1 : 

  (sCb === 'LA' && sCa !== 'LA') || (sCb === 'NY' && sCa === 'CH') ? 1 : 0;

});


console.log(sorted);


查看完整回答
反對 回復 2022-09-23
?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

data = [

  {

    stateCode: 'CH',

    startDate: new Date('06/12/2020 08:00'),

    startDateWithDelay: new Date('06/12/2020 08:00'),

  },

  {

    stateCode: 'LA',

    startDate: new Date('06/12/2020 08:00'),

    startDateWithDelay: new Date('06/12/2020 08:30'),

  },

  {

    stateCode: 'NY',

    startDate: new Date('06/12/2020 06:00'),

    startDateWithDelay: new Date('06/12/2020 06:00')

  }

]

我們可以使用數組的內置排序函數,因此我們將比較 startDate 如果它們相同,我們將在 startDate 上排序,否則繼續在 startDate 上排序。


let data2 = data.sort((s1, s2) => s1.startDate == s2.startDate ? 

            s1.startDateWithDelay - s2.startDateWithDelay : 

            s1.startDate - s2.startDate); 

排序完成后,我們可以使用map來修改結果,并加入預期輸出的排序狀態列表。


let states = data2.map(s => s.stateCode).join(', ');

這將輸出預期的結果。


NY, CH, LA


查看完整回答
反對 回復 2022-09-23
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

你可以做這樣的事情


data = [

  {

    stateCode: 'CH',

    startDate: new Date('06/12/2020 08:00'),

    startDateWithDelay: new Date('06/12/2020 08:00'),

  },

  {

    stateCode: 'LA',

    startDate: new Date('06/12/2020 08:00'),

    startDateWithDelay: new Date('06/12/2020 08:30'),

  },

  {

    stateCode: 'NY',

    startDate: new Date('06/12/2020 06:00'),

    startDateWithDelay: new Date('06/12/2020 06:00')

  }

];


data.sort((a,b)=>{

 if(a.startDate.getTime() < b.startDate.getTime()){

  return -1

 }else if(a.startDate.getTime() < b.startDate.getTime()){

  return 1

 }else{

  return a.startDateWithDelay.getTime() - b.startDateWithDelay.getTime()

 }

});


console.log(data)


查看完整回答
反對 回復 2022-09-23
  • 4 回答
  • 0 關注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號