Man*_*ppa -1 javascript sorting compiler-errors typescript
有谁能够帮我.
我试图从数组中排序字符串
var someArray= ["3a445a_V1", "3", "2a33s454_V1", "1", "3_V2", "2s4a34s3_V2", "234343"];
const [record] = someArray.map(r => parseFloat(r.replace('_V','.'))).sort((a,b) => a < b);
console.log(record)
//it returns 3a445a.1
Run Code Online (Sandbox Code Playgroud)
在浏览器console.log中它工作正常,而不是打字稿?
打字稿,它给出了以下错误 错误:
error TS2345: Argument of type '(a: number, b: number) => boolean' is not
assignable to parameter of type '(a: number, b: number) => number'.
Type 'boolean' is not assignable to type 'number'.
Run Code Online (Sandbox Code Playgroud)
任何的想法?提前致谢
.sort((a,b) => a < b)是不正确的.该sort回调应该返回一个号码,而不是一个布尔-正是打字稿告诉你的.
相反:.sort((a,b) => a - b)(-代替<).或者b - a以另一种方式排序.
| 归档时间: |
|
| 查看次数: |
2810 次 |
| 最近记录: |