把url里的query参数转成对象格式

const urlString='http"//www.xxx.com?name=tom&age=18&height=180'
// 1.拿到?后的那部分
const queryString=urlString.split('?')
console.log(queryString[1])//name=tom&age=18&height=180
const queryParams=queryString[1]
// 2.变成[key,value],[],[]形式
const queryParamsArr=new URLSearchParams(queryParams)
console.log(queryParamsArr)//URLSearchParams { 'name' => 'tom', 'age' => '18', 'height' => '180' }
// 3.变成对象 {key:value,..}
const paramsObj=Object.fromEntries(queryParamsArr)
console.log(paramsObj)//{ name: 'tom', age: '18', height: '180' }

猜你喜欢

转载自blog.csdn.net/weixin_51290060/article/details/132087637