// Input: "https://jsrun.net/new?key1=value1&key2=value2";
// Output: {
// key1:"value1",
// key2:"value2"
// }
let input = "https://jsrun.net/new?key1=value1&key2=value2"
const index = input.indexOf('?')
const obj = {}
// 说明存在参数
if (index > 0) {
let all_query = input.split('?')[1]
let query_arr = all_query.split('&')
query_arr.forEach(item => {
const item_arr = item.split("=")
obj[item_arr[0]] = item_arr[1]
})
}
console.log(obj)
实现url截取参数转换成对象
最新推荐文章于 2024-04-03 15:46:51 发布
该文章介绍了如何使用JavaScript解析URL中的查询参数,通过split()函数将URL中的问号后的部分分解成键值对,并存储在对象中。
摘要由CSDN通过智能技术生成