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

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

按不同的鍵重新排序已排序的數組

按不同的鍵重新排序已排序的數組

叮當貓咪 2023-03-03 15:46:57
我有這個數組,已經按上傳時間排序    [      { id: 1133, userId: 101697, uploadTime: '2020-09-14T16:19:16.000Z' },      { id: 1132, userId: 101543, uploadTime: '2020-09-14T16:17:27.000Z' },      { id: 1131, userId: 101697, uploadTime: '2020-09-14T16:09:49.000Z' },      { id: 1130, userId: 101697, uploadTime: '2020-09-14T16:09:16.000Z' },      { id: 1128, userId: 101543, uploadTime: '2020-09-09T21:48:46.000Z' },      { id: 1127, userId: 101543, uploadTime: '2020-09-09T21:41:41.000Z' },      { id: 1126, userId: 101543, uploadTime: '2020-09-09T21:35:37.000Z' },      { id: 1125, userId: 101543, uploadTime: '2020-09-09T19:41:57.000Z' },      { id: 1082, userId: 101577, uploadTime: '2020-08-21T15:24:35.000Z' },      { id: 1049, userId: 101589, uploadTime: '2020-08-15T23:31:42.000Z' },      { id: 1040, userId: 101589, uploadTime: '2020-08-14T15:04:01.000Z' },      { id: 1036, userId: 101589, uploadTime: '2020-08-13T00:09:43.000Z' },      { id: 1035, userId: 101527, uploadTime: '2020-08-12T20:07:34.000Z' },      { id: 1034, userId: 101612, uploadTime: '2020-08-12T17:33:26.000Z' },      { id: 996, userId: 101589, uploadTime: '2020-08-11T15:20:11.000Z' },       { id: 889, userId: 101626, uploadTime: '2020-08-06T14:47:18.000Z' },       { id: 864, userId: 101589, uploadTime: '2020-08-05T15:23:50.000Z' },       { id: 863, userId: 101589, uploadTime: '2020-08-05T14:42:14.000Z' },       { id: 852, userId: 101589, uploadTime: '2020-08-04T18:05:07.000Z' },       { id: 851, userId: 101589, uploadTime: '2020-08-04T17:57:58.000Z' }    ]現在我想求助于它,但僅適用于具有相同 userId 且 uploadTime 距當前時間不到 24 小時的對象。輸出應該是這樣的:    [      { id: 1133, userId: 101697, uploadTime: '2020-09-14T16:19:16.000Z' },      { id: 1131, userId: 101697, uploadTime: '2020-09-14T16:09:49.000Z' },      { id: 1130, userId: 101697, uploadTime: '2020-09-14T16:09:16.000Z' },      { id: 1132, userId: 101543, uploadTime: '2020-09-14T16:17:27.000Z' },    ]
查看完整描述

4 回答

?
holdtom

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

現在我想求助于它,但僅適用于具有相同 userId 且 uploadTime 距當前時間不到 24 小時的對象。


這次排序需要稍微改變一下(看代碼中的注釋):


如果兩個日期都小于 24 小時,則按userId排序,否則繼續使用 uploadTime 以保留順序。


var data = [

    {id: 1133, userId: 101697, uploadTime: '2020-09-14T16:19:16.000Z'},

    {id: 1132, userId: 101543, uploadTime: '2020-09-14T16:17:27.000Z'},

    {id: 1131, userId: 101697, uploadTime: '2020-09-14T16:09:49.000Z'},

    {id: 1130, userId: 101697, uploadTime: '2020-09-14T16:09:16.000Z'},

    {id: 1128, userId: 101543, uploadTime: '2020-09-09T21:48:46.000Z'},

    {id: 1127, userId: 101543, uploadTime: '2020-09-09T21:41:41.000Z'},

    {id: 1126, userId: 101543, uploadTime: '2020-09-09T21:35:37.000Z'},

    {id: 1125, userId: 101543, uploadTime: '2020-09-09T19:41:57.000Z'},

    {id: 1082, userId: 101577, uploadTime: '2020-08-21T15:24:35.000Z'},

    {id: 1049, userId: 101589, uploadTime: '2020-08-15T23:31:42.000Z'},

    {id: 1040, userId: 101589, uploadTime: '2020-08-14T15:04:01.000Z'},

    {id: 1036, userId: 101589, uploadTime: '2020-08-13T00:09:43.000Z'},

    {id: 1035, userId: 101527, uploadTime: '2020-08-12T20:07:34.000Z'},

    {id: 1034, userId: 101612, uploadTime: '2020-08-12T17:33:26.000Z'},

    {id: 996, userId: 101589, uploadTime: '2020-08-11T15:20:11.000Z'},

    {id: 889, userId: 101626, uploadTime: '2020-08-06T14:47:18.000Z'},

    {id: 864, userId: 101589, uploadTime: '2020-08-05T15:23:50.000Z'},

    {id: 863, userId: 101589, uploadTime: '2020-08-05T14:42:14.000Z'},

    {id: 852, userId: 101589, uploadTime: '2020-08-04T18:05:07.000Z'},

    {id: 851, userId: 101589, uploadTime: '2020-08-04T17:57:58.000Z'}

];


data.sort(function (a, b) {

    // compute the difference with current date

    var dcta = (new Date()).getTime() - (new Date(a.uploadTime)).getTime();

    var dctb = (new Date()).getTime() - (new Date(b.uploadTime)).getTime();


   // convert then in hours

    var h1 = (((dctb) / 1000) / 60)/60;

    var h2 = ((dctb / 1000) / 60)/60;


    // if both are less than 24hours....    

    if (h1 <=24 && h2 <= 24) {

        return b.userId - a.userId;

    } else {

        return (new Date(b.uploadTime)).getTime() - (new Date(a.uploadTime)).getTime();

    }

});

console.log(JSON.stringify(data).replace(/\},/g, '},\n'));


查看完整回答
反對 回復 2023-03-03
?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

let testArray = [

    { id: 1133, userId: 101697, uploadTime: '2020-09-14T16:19:16.000Z' },

    { id: 1132, userId: 101543, uploadTime: '2020-09-14T16:17:27.000Z' },

    { id: 1131, userId: 101697, uploadTime: '2020-09-14T16:09:49.000Z' },

    { id: 1130, userId: 101697, uploadTime: '2020-09-14T16:09:16.000Z' },

    { id: 1128, userId: 101543, uploadTime: '2020-09-09T21:48:46.000Z' },

    { id: 1127, userId: 101543, uploadTime: '2020-09-09T21:41:41.000Z' },

    { id: 1126, userId: 101543, uploadTime: '2020-09-09T21:35:37.000Z' },

    { id: 1125, userId: 101543, uploadTime: '2020-09-09T19:41:57.000Z' },

    { id: 1082, userId: 101577, uploadTime: '2020-08-21T15:24:35.000Z' },

    { id: 1049, userId: 101589, uploadTime: '2020-08-15T23:31:42.000Z' },

    { id: 1040, userId: 101589, uploadTime: '2020-08-14T15:04:01.000Z' },

    { id: 1036, userId: 101589, uploadTime: '2020-08-13T00:09:43.000Z' },

    { id: 1035, userId: 101527, uploadTime: '2020-08-12T20:07:34.000Z' },

    { id: 1034, userId: 101612, uploadTime: '2020-08-12T17:33:26.000Z' },

    { id: 996, userId: 101589, uploadTime: '2020-08-11T15:20:11.000Z' },

    { id: 889, userId: 101626, uploadTime: '2020-08-06T14:47:18.000Z' },

    { id: 864, userId: 101589, uploadTime: '2020-08-05T15:23:50.000Z' },

    { id: 863, userId: 101589, uploadTime: '2020-08-05T14:42:14.000Z' },

    { id: 852, userId: 101589, uploadTime: '2020-08-04T18:05:07.000Z' },

    { id: 851, userId: 101589, uploadTime: '2020-08-04T17:57:58.000Z' }

];





testArray = testArray.sort((x, y) => {

    return (y.userId - x.userId);

});


testArray = testArray.filter(currentObj => {

    return (new Date() - new Date(currentObj.uploadTime)) < 24 * 60 * 60 * 1000;

})


    

    

    console.log(testArray);



 

我們排序比較對象的兩個不同屬性?,F在您將按 userId 對用戶進行排序,然后按從最新到最舊的上傳時間排序。


結果:


[ { id: 1133,

    userId: 101697,

    uploadTime: '2020-09-14T16:19:16.000Z' },

  { id: 1131,

    userId: 101697,

    uploadTime: '2020-09-14T16:09:49.000Z' },

  { id: 1130,

    userId: 101697,

    uploadTime: '2020-09-14T16:09:16.000Z' },

  { id: 1132,

    userId: 101543,

    uploadTime: '2020-09-14T16:17:27.000Z' } ]


查看完整回答
反對 回復 2023-03-03
?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

var uploads=[{"id":1035,"userId":101527,"uploadTime":"2020-08-12T20:07:34.000Z"},{"id":1125,"userId":101543,"uploadTime":"2020-09-09T19:41:57.000Z"},{"id":1126,"userId":101543,"uploadTime":"2020-09-09T21:35:37.000Z"},{"id":1127,"userId":101543,"uploadTime":"2020-09-09T21:41:41.000Z"},{"id":1128,"userId":101543,"uploadTime":"2020-09-09T21:48:46.000Z"},{"id":1132,"userId":101543,"uploadTime":"2020-09-14T16:17:27.000Z"},{"id":1082,"userId":101577,"uploadTime":"2020-08-21T15:24:35.000Z"},{"id":851,"userId":101589,"uploadTime":"2020-08-04T17:57:58.000Z"},{"id":852,"userId":101589,"uploadTime":"2020-08-04T18:05:07.000Z"},{"id":863,"userId":101589,"uploadTime":"2020-08-05T14:42:14.000Z"},{"id":864,"userId":101589,"uploadTime":"2020-08-05T15:23:50.000Z"},{"id":996,"userId":101589,"uploadTime":"2020-08-11T15:20:11.000Z"},{"id":1036,"userId":101589,"uploadTime":"2020-08-13T00:09:43.000Z"},{"id":1040,"userId":101589,"uploadTime":"2020-08-14T15:04:01.000Z"},{"id":1049,"userId":101589,"uploadTime":"2020-08-15T23:31:42.000Z"},{"id":1034,"userId":101612,"uploadTime":"2020-08-12T17:33:26.000Z"},{"id":889,"userId":101626,"uploadTime":"2020-08-06T14:47:18.000Z"},{"id":1130,"userId":101697,"uploadTime":"2020-09-14T16:09:16.000Z"},{"id":1131,"userId":101697,"uploadTime":"2020-09-14T16:09:49.000Z"},{"id":1133,"userId":101697,"uploadTime":"2020-09-14T16:19:16.000Z"}];


var last24hoursByUserId = 

  uploads

  .filter(upload=>

    new Date() - new Date(upload.uploadTime) 

    <

    24*60*60*1000

  )

  .sort((a,b)=>

    a.userId+a.uploadTime<b.userId+b.uploadTime?-1:1

  )

;


console.log(last24hoursByUserId);

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反對 回復 2023-03-03
?
慕村9548890

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

這個答案首先解析字符串uploadTime。然后我檢查是否uploadTime在之后yesterday并將其存儲為isWithin24Hours. 我將此數據保存在一個單獨的對象中sortData,以免污染或改變testArray.


const testArray = [

    { id: 1133, userId: 101697, uploadTime: '2020-09-14T16:19:16.000Z' },

    { id: 1132, userId: 101543, uploadTime: '2020-09-14T16:17:27.000Z' },

    { id: 1131, userId: 101697, uploadTime: '2020-09-14T16:09:49.000Z' },

    { id: 1130, userId: 101697, uploadTime: '2020-09-14T16:09:16.000Z' },

    { id: 1128, userId: 101543, uploadTime: '2020-09-09T21:48:46.000Z' },

    { id: 1127, userId: 101543, uploadTime: '2020-09-09T21:41:41.000Z' },

    { id: 1126, userId: 101543, uploadTime: '2020-09-09T21:35:37.000Z' },

    { id: 1125, userId: 101543, uploadTime: '2020-09-09T19:41:57.000Z' },

    { id: 1082, userId: 101577, uploadTime: '2020-08-21T15:24:35.000Z' },

    { id: 1049, userId: 101589, uploadTime: '2020-08-15T23:31:42.000Z' },

    { id: 1040, userId: 101589, uploadTime: '2020-08-14T15:04:01.000Z' },

    { id: 1036, userId: 101589, uploadTime: '2020-08-13T00:09:43.000Z' },

    { id: 1035, userId: 101527, uploadTime: '2020-08-12T20:07:34.000Z' },

    { id: 1034, userId: 101612, uploadTime: '2020-08-12T17:33:26.000Z' },

    { id: 996, userId: 101589, uploadTime: '2020-08-11T15:20:11.000Z' },

    { id: 889, userId: 101626, uploadTime: '2020-08-06T14:47:18.000Z' },

    { id: 864, userId: 101589, uploadTime: '2020-08-05T15:23:50.000Z' },

    { id: 863, userId: 101589, uploadTime: '2020-08-05T14:42:14.000Z' },

    { id: 852, userId: 101589, uploadTime: '2020-08-04T18:05:07.000Z' },

    { id: 851, userId: 101589, uploadTime: '2020-08-04T17:57:58.000Z' }

];


// use timestamp to produces the same output for future readers

const now = 1600122461595 // Date.now(); 

const yesterday = now - (24 * 60 * 60 * 1000);


// create complementary data needed for sort

const sortData  = new Map(testArray.map(({id, uploadTime}) => {

  uploadTime = Date.parse(uploadTime);

  return [id, { uploadTime, isWithin24Hours: uploadTime > yesterday }];

}));


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

  const _a = sortData.get(a.id), _b = sortData.get(b.id);

  return _b.isWithin24Hours - _a.isWithin24Hours   // isWithin24Hours DESC

      || _a.isWithin24Hours && b.userId - a.userId // userId DESC if isWithin24Hours = 1

      || _b.uploadTime - _a.uploadTime;            // uploadTime DESC

});


console.log(testArray);

console.table(testArray); // check browser console for this output


我使用_b.isWithin24Hours - _a.isWithin24Hours( isWithing24Hours DESC) 將 24 小時內的記錄移到前面。_a.isWithin24Hours此檢查用作 XOR,如果和的值_b.isWithin24Hours彼此不同,將返回。


下一個檢查是_a.isWithin24Hours && b.userId - a.userId。_a.isWithin24Hours如果兩者都不在 24 小時內(由于上一次檢查,兩者相同),將繼續進行下一次檢查。但是,如果兩者都在 24 小時內,則使用check b.userId - a.userId( )。userId DESC


如果兩者都在 24 小時內并且具有相同的,或者兩者都不在 24 小時內,則使用最后一次檢查_b.uploadTime - _a.uploadTime( )。uploadTime DESCuserId


查看完整回答
反對 回復 2023-03-03
  • 4 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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