js 对象与 queryString 相互转换

对象转queryString

1
(new URLSearchParams(obj)).toString()

queryString 转对象

1
2
3
4
5
6
7
8
9
10
const getQueryObj = () => {
const query = window.location.href.split('?')[1]
if (!query) return {}
const obj = {}
query.split('&').forEach(q => {
const item = q.split('=')
obj[item[0]] = item[1]
})
return obj
}