js|js url中的参数与对象互转

url参数转对象

urlToToObj = (url) => { const obj = {}; if (url) { const params = url.split('?')[1].split('&'); params.map(item => obj[item.split("=")[0]] = item.split("=")[1]); } return obj; // {abc: '1', type: '2'} }

对象转url
objToUrl = (obj) => { const tempArray = []; for (const item in obj) { if (item) { tempArray.push(`${item}=${obj[item]}`); } } return `https://www.xxx.com/xxx?${tempArray.join('&')}`; //https://www.xxx.com/xxx?abc=1&type=2 }

    推荐阅读