创建

  1. // 地址
  2. let url = new URL('https://example.com?foo=1&bar=2')
  3. let params = new URLSearchParams(url.search)
  4. for (let v of params) {
  5. console.log(v)
  6. }
  7. // map结构、 二维数组
  8. let map = new Map()
  9. map.set('name', '赵敏')
  10. map.set('sex', 'woman')
  11. for (let v of map) {
  12. console.log(v)
  13. }
  14. // 对象
  15. let person = {
  16. name: '张无忌',
  17. age: 20,
  18. skill: '乾坤大挪移'
  19. }
  20. let personParams = new URLSearchParams(person)
  21. for (let v of personParams) {
  22. console.log(v)
  23. }

方法

append()

  1. let url = new URL('https://example.com?foo=1&bar=2')
  2. let params = new URLSearchParams(url.search)
  3. params.append('topic', 3)

delete()

  1. let url = new URL('https://example.com?foo=1&bar=2')
  2. let params = new URLSearchParams(url.search)
  3. params.delete(topic)

entries()

keys()

values()

get()

getAll()

has()

set()

// 如果没有,则添加;如果有,则替换

  1. let url = new URL('https://example.com?foo=1&bar=2')
  2. let params = new URLSearchParams(url.search)
  3. params.set('topic', 18)

sort()

toString()