new URLSearchParams(obj)


  • obj <Object> 表示键值对集合的对象

    ¥obj <Object> An object representing a collection of key-value pairs

使用查询哈希映射实例化新的 URLSearchParams 对象。obj 的每个属性的键和值总是被强制转换为字符串。

¥Instantiate a new URLSearchParams object with a query hash map. The key and value of each property of obj are always coerced to strings.

querystring 模块不同,不允许以数组值的形式出现重复的键。数组使用 array.toString() 字符串化,它简单地用逗号连接所有数组元素。

¥Unlike querystring module, duplicate keys in the form of array values are not allowed. Arrays are stringified using array.toString(), which simply joins all array elements with commas.

const params = new URLSearchParams({
  user: 'abc',
  query: ['first', 'second'],
});
console.log(params.getAll('query'));
// Prints [ 'first,second' ]
console.log(params.toString());
// Prints 'user=abc&query=first%2Csecond'