彻底搞清楚split,slice,join。常用数组字符串处理

61 阅读1分钟

split

这边用url地址来举例

http://localhost:8080/#/?shop_id=2&name=xx

var spl = window.location.href.split("#")
括号内传什么,就从哪里分割,也可以接数组
("#")【0】。("#")【1】这样使用分割后的数据
分割字符串为数组
['http://localhost:8080/', '/?shop_id=2&name=xx']

join

这边用url地址来举例

http://localhost:8080/#/?shop_id=2&name=xx

var spl = window.location.href.split("#")
数组转为字符串
['http://localhost:8080/', '/?shop_id=2&name=xx']

let splJoin = spl.join()
http://localhost:8080//?shop_id=2&name=xx

slice

这边用url地址来举例

http://localhost:8080/#/?shop_id=2&name=xx

var spl = window.location.href.split("#")
数组转为字符串
['http://localhost:8080/', '/?shop_id=2&name=xx']

let splJoin = spl.join()
http://localhost:8080//?shop_id=2&name=xx

var url1 = splJoin.slice(splJoin.indexOf('?') + 1)
slice(0,1) 从index0的位置截取到1.不写第二个参数默认截到尾部
 shop_id=2&name=xx