当前位置: 首页 > Web前端 > JavaScript

jsurl中的参数转为对象

时间:2023-03-27 13:32:18 JavaScript

url参数转为对象urlToToObj=(url)=>{constobj={};如果(url){constparams=url.split('?')[1]。分裂('&');params.map(item=>obj[item.split("=")[0]]=item.split("=")[1]);}返回对象;//{abc:'1',type:'2'}}对象到urlobjToUrl=(obj)=>{consttempArray=[];for(constiteminobj){if(item){tempArray.push(`${item}=${obj[item]}`);}}返回`https://www.xxx.com/xxx?${tempArray.join('&')}`;//https://www.xxx.com/xxx?abc=1&type=2}