typescript
js
# 常用操作
# 保留两位小数
Math.floor(15.7784514000 * 100) / 100 // 输出结果为 15.77
parseInt(7/2) // 丢弃小数部分,保留整数部分
const a = 2.461;
const b = (parseInt(a * 100) / 100).toFixed(2); // 2.46
1
2
3
4
5
2
3
4
5
# 创建Map并赋值
const map = new Map([[2, 'foo'], [1, 'bar']]);
1
# 判断Map类型
isMap(obj) {
return obj instanceof Map;
}
1
2
3
2
3
# 删除数组中的某个元素
const arr = ['a','b','c','d'];
arr.splice(1,1);
console.log(arr); //['a','c','d'];
1
2
3
2
3
# Object转成Map
const result = new Map(Object.entries(map));
1
# 将Map的key、value转成数组
const values = Array.from(map.values());
const keys = Array.from(map.keys());
1
2
2
# 求数组最大值
const maxN = Math.max.apply(null,ary);
const minN = Math.min.apply(null,ary);
1
2
2
# 实现Map的深克隆
export class MapUtils {
public static deepClone(obj) {
if (!obj || true === obj) { // this also handles boolean as true and false
return obj;
}
const objType = typeof(obj);
if ('number' === objType || 'string' === objType) { // add your immutables here
return obj;
}
const result = Array.isArray(obj) ? [] : !obj.constructor ? {} : new obj.constructor();
if (obj instanceof Map) {
for (const key of obj.keys()) {
result.set(key, this.deepClone(obj.get(key)));
}
}
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
result[key] = this.deepClone(obj[key]);
}
}
return result;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 请求的url后面添加时间戳参数或者随机数的参数
// 添加时间戳
...?time=new Date();
// 添加随机数
...?number=Math.random();
//
"...?" + Math.floor(Math.random() * 100)
1
2
3
4
5
6
2
3
4
5
6