我有一个使用 Underscore 的数组函数 _.sortBy()
排序的数组,我需要将其转换为普通 JS 函数 .sort()
。
我的问题是,当我将其转换为普通 array.sort()
函数时,我的 IDE (Webstorm) 抛出错误:
TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
下划线版本
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));
有人可以帮我看看这里可能出了什么问题吗?
PS-请参见上图中new Date(a.timeslot)
中的红色下划线
最佳答案
试试这个:
this.interviewDetails.data.sort((a: any, b: any) => new Date(a.timeslot).getTime() - new Date(b.timeslot).getTime());
关于javascript - 将下划线 sortBy 转换为 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59101546/