Node中的 URLSearchParams.get 和 getAll()
简介get()
此函数返回与参数中传递的名称匹配的第一个名称-值对。如果不存在这样的值,则返回null。如果存在多个名称-值对,它将返回该元素在URL中的第一次出现。
语法
URLSearchParams.get(name);
它将返回与作为函数参数传递的名称匹配的单个字符串。如果不存在对,则返回null。
示例1
// 将 URL 定义为常量
const myURL = new URL(
'https://example.org/?firstName=John&firstName=Mark');
// Printing all the params that match value -> 'firstName'
console.log(myURL.searchParams.get('firstName'));输出
John
示例2((当参数值不存在时))
// 将 URL 定义为常量
const myURL = new URL(
'https://example.org/?firstName=John&firstName=Mark');
// Printing all the params that match value -> 'lastName'
console.log(myURL.searchParams.getAll('lastName'));输出
null
简介getAll()
此函数返回与给定参数匹配的所有值。如果不存在这样的对,则返回null。如果存在多个名称-值对,它将以数组格式返回该元素的所有出现。
语法
URLSearchParams.getAll(name);
它将返回一个字符串数组,其名称-值对与在函数中作为参数传递的名称相匹配。如果不存在对,它将返回一个空数组。
示例1
// 将 URL 定义为常量
const myURL = new URL(
'https://example.org/?firstName=John&firstName=Mark');
// Printing all the params that match value -> 'firstName'
console.log(myURL.searchParams.getAll('firstName'));输出
['John', 'Mark']
示例2
// 将 URL 定义为常量
const myURL = new URL(
'https://example.org/?Id=2&Id=3&Id=7');
// Printing all the params that match value -> 'Id'
console.log(myURL.searchParams.getAll('Id'));输出
['2', '3', '7']
注–支持所有现代浏览器。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下