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']
注–支持所有现代浏览器。
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短