将下划线sortBy转换为Javascript

时间:2019-11-29 08:52:46

标签: javascript typescript underscore.js

我有一个数组,该数组使用Underscore的数组函数_.sortBy()进行了排序,我需要将其转换为普通的JS函数.sort()

我的问题是,当我将其转换为普通的香草array.sort()函数时,我的IDE(Webstorm)抛出错误,如下所示:

  

TS2362:算术运算的左侧必须为'any','number','bigint'或枚举类型。

下划线版本

this.interviewDetails.data = _.sortBy(this.interviewDetails.data, function (o) {
   return new Date(o.timeslot);
});

香草版本

this.interviewDetails.data.sort((a: any, b: any) => new Date(a.timeslot) - new Date(b.timeslot));

enter image description here

有人可以帮助我这里有什么问题吗?

PS-,请参见上图new Date(a.timeslot)中的红色下划线

1 个答案:

答案 0 :(得分:4)

尝试:

this.interviewDetails.data.sort((a: any, b: any) => new Date(a.timeslot).getTime() - new Date(b.timeslot).getTime());