JavaScript 数组方法到 AutoHotkey 的转换
AutoHotkey 缺乏内置的迭代辅助方法(截至 1.1.33)来执行其他语言中常见的许多常见数组行为。
移植方法
- concat
- every
- fill
- filter
- find
- findIndex
- forEach
- includes
- indexOf
- join
- lastIndexOf
- map
- reduce
- reduceRight
- reverse
- shift
- slice
- some
- sort
- splice
- toString
- unshift
安装
Code: Select all
#Include array.ahk\export.ahk
msgbox, % [1,2,3].join()
; => "1,2,3"
用法
array.<fn>([params*])
Code: Select all
; 映射到双倍值
arrayInt := [1, 5, 10]
arrayInt.map(func("fn_doubleInt"))
; => [2, 10, 20]
fn_doubleInt(int) {
return int * 2
}
; 映射到对象属性
arrayObj := [{"name": "bob", "age": 22}, {"name": "tom", "age": 51}]
arrayObj.map(func("fn_returnName"))
; => ["bob", "tom"]
fn_returnName(obj) {
return obj.name
}
; 方法链接
arrayObj := [{"name": "bob", "age": 22}, {"name": "tom", "age": 51}]
msgbox, % arrayObj.map(func("fn_returnProp").bind("age"))
.map(func("fn_doubleInt"))
.join(",")
; => "44,102"
fn_returnProp(prop, obj) {
return obj[prop]
}
排序
JavaScript 不公开开始/结束或左/右参数,这种排序也不公开。
array.sort([params*])
Code: Select all
arrayInt := [11,9,5,10,1,6,3,4,7,8,2]
arrayInt.sort()
; => [1,2,3,4,5,6,7,8,9,10,11]
我觉得翻译文档页面会很有趣。结果比原先预计的工作量大得多,不过现在已经完成了!
如果您发现错误或需要改进的地方,请向 /docs/translations.csv 提出拉取请求。