Home > @silence_zhpf/utils > HashMap > map
这是一个 TypeScript 函数,它映射HashMap
并根据提供的函数返回不同类型的新数组。
Signature:
map<R>(fn: (value: [T, U], index: number, array: Array<[T, U]>) => R): R[];
Parameter | Type | Description |
---|---|---|
fn | (value: [T, U], index: number, array: Array<[T, U]>) => R | 接受三个参数的函数:第一个参数是一个类型为[T,U] 的元组,第二个参数是一个整数,第三个参数是一个数组。 |
Returns:
R[]
map
方法返回一个类型为 R[]
的数组
因为HashMap
的Map方法返回的是一个R
类型的数组,为了可以在链式调用中可以再使用HashMap
,可以利用数组的reduce
方式重新把R
类型的数组转为HashMap
对象。
const hashMap = HashMap.new([[1,2],[3,4],[5,6]]);
hashMap.map(([a,b])=>a+b)
.reduce((acc,cur)=>{
acc.insert(cur,`${cur}`);
return acc;
},HashMap.new<number,string>())