简体   繁体   English

将下划线 sortBy 转换为 javascript

[英]Convert underscore sortBy to javasscript

I have an array which was sorted using Underscore's array function _.sortBy() , which I need to convert it into a vanilla JS function .sort() .我有一个使用_.sortBy()的数组函数_.sortBy()排序的数组,我需要将它转换为普通的 JS 函数.sort()

My problem is when I convert it to normal vanilla array.sort() function, my IDE (Webstorm) is throwing an error as:我的问题是当我将它转换为普通的 vanilla array.sort()函数时,我的 IDE (Webstorm) 抛出一个错误:

TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type. TS2362:算术运算的左侧必须是“any”、“number”、“bigint”或枚举类型的类型。

Underscore version下划线版本

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

Vanilla version香草版

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

在此处输入图片说明

Can anybody help me what's possibly wrong here?任何人都可以帮助我这里可能有什么问题吗?

PS- Please see the red underlines in new Date(a.timeslot) in the image above PS-请参阅new Date(a.timeslot)中的红色下划线

尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM