javascript 如何按键嵌套数组对数组排序?

1dkrff03  于 2023-02-02  发布在  Java
关注(0)|答案(2)|浏览(161)

我有下面的数组

var dic = [
  {user:'John', notifications:[{created: 'Wed Jan 12 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, 
                              {created: 'Wed Feb 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Ron', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'xxx', notifications:[{created: 'Wed Jan 31 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
]

我的目标是按日期对所有通知进行排序-最终结果应该如下所示-“Bob”应该是第一个,因为他获得了更高的日期(2月15日)

var dic = [
  {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, 
                              {created: 'Wed Feb 15 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'John', notifications:[{created: 'Wed Jan 12 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, 
                              {created: 'Wed Feb 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Ron', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},

]

我试着像示例中那样对它进行排序-但我没有成功。

var dic = [
  {user:'John', notifications:[{created: 'Wed Jan 12 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, {created: 'Wed Feb 15 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Ron', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
 {user:'xxx', notifications:[{created: 'Wed Jan 31 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
 
]

dic.sort((a,b) => {
 //console.log('a', a, 'b', b);
  if(a.notifications.length > 1){
    const test = a.notifications.reduce((a,b) => new Date(a.created).getTime() >= new Date(b.created).getTime() ? a.created : b.created);
    return new Date(test).getTime() >= new Date(b.notifications[0].created).getTime() ? -1 : 1;

  } else{
    return new Date(a.notifications[0].created).getTime() <= new Date(b.notifications[0].created).getTime() ? -1 : 1;

  }

})

console.log(dic)
ilmyapht

ilmyapht1#

这看起来像您希望的那样工作:

var dic = [
  {user:'John', notifications:[{created: 'Wed Jan 12 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, {created: 'Wed Feb 15 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Ron', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
 {user:'xxx', notifications:[{created: 'Wed Jan 31 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
 {user:'no notifications', notifications:[]}
]

function getMaxDate(n){
  return n.notifications.reduce( (max, d) => (d = new Date(d.created).getTime(), !max || d > max ? d : max), null) ?? Date.now()
}

dic.sort((a,b) => {
  return getMaxDate(b) - getMaxDate(a)

})

console.log(dic)

只需从每个对象获取日期,不需要if

laawzig2

laawzig22#

var dic = [
  {user:'John', notifications:[{created: 'Wed Jan 12 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, {created: 'Wed Feb 15 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Ron', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
 {user:'xxx', notifications:[{created: 'Wed Jan 31 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
 
]

dic.sort((a,b) => {
let aSmall = a.notifications.sort((aa,bb) => { return new Date(aa.created) - new Date(bb.created) })[0];
let bSmall = b.notifications.sort((aa,bb) => { return new Date(aa.created) - new Date(bb.created) })[0];

// console.log(new Date(aSmall.created) - new Date(bSmall.created))
return new Date(aSmall.created) - new Date(bSmall.created)
})

console.log(dic)

相关问题