常用数组操作

127 阅读25分钟

日常编码中有很多场景需要对数组进行处理,故此想通过一遍文章总结下常用的一些方法

replace替换用法

1.全局替换

全局替换cover中的'/'和':'字符,

cover = cover.replace(new RegExp('/','g'),'%2F')      
cover = cover.replace(new RegExp(':','g'),'%3A')

得到结果:

2.局部替换

http://zhyq.szns.gov.cn/MCA/LEAP/msdist/index.html#/home/suggestion/propose/recommend

如果想替换掉home之后的字段,可通过:

替换后成为:

http://zhyq.szns.gov.cn/MCA/LEAP/msdist/index.html#/active/sign?id=5105bb4baca949628b208b5c8f0d69fa

JS数组元素String与Number类型的快速转换

1.String类型转换为Number类型

let x1 = ['1','2'];
console.log(x1);
//["1", "2"]
x1 = x1.map(Number);
console.log(x1);
//[1,2]

**1.Number类型转换为String类型  **

let x1 = [1,2];
console.log(x1);
//[1, 2]
x1 = x1.map(string);
console.log(x1);
//['1','2']