utils

Home > @silence_zhpf/utils > HashMap > map

HashMap.map() method

这是一个 TypeScript 函数,它映射HashMap并根据提供的函数返回不同类型的新数组。

Signature:

map<R>(fn: (value: [T, U], index: number, array: Array<[T, U]>) => R): R[];

Parameters

Parameter Type Description
fn (value: [T, U], index: number, array: Array<[T, U]>) => R 接受三个参数的函数:第一个参数是一个类型为[T,U]的元组,第二个参数是一个整数,第三个参数是一个数组。

Returns:

R[]

map 方法返回一个类型为 R[] 的数组

Remarks

因为HashMap的Map方法返回的是一个R类型的数组,为了可以在链式调用中可以再使用HashMap,可以利用数组的reduce方式重新把R类型的数组转为HashMap对象。

Example

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>())