new URLSearchParams(obj)


  • obj <Object> 表示一组键值对的对象

使用查询哈希映射实例化一个新的 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'