创建
// 地址
let url = new URL('https://example.com?foo=1&bar=2')
let params = new URLSearchParams(url.search)
for (let v of params) {
console.log(v)
}
// map结构、 二维数组
let map = new Map()
map.set('name', '赵敏')
map.set('sex', 'woman')
for (let v of map) {
console.log(v)
}
// 对象
let person = {
name: '张无忌',
age: 20,
skill: '乾坤大挪移'
}
let personParams = new URLSearchParams(person)
for (let v of personParams) {
console.log(v)
}
方法
append()
let url = new URL('https://example.com?foo=1&bar=2')
let params = new URLSearchParams(url.search)
params.append('topic', 3)
delete()
let url = new URL('https://example.com?foo=1&bar=2')
let params = new URLSearchParams(url.search)
params.delete(topic)
entries()
keys()
values()
get()
getAll()
has()
set()
// 如果没有,则添加;如果有,则替换
let url = new URL('https://example.com?foo=1&bar=2')
let params = new URLSearchParams(url.search)
params.set('topic', 18)