new URLSearchParams(obj)
使用查询哈希映射实例化新的 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'