URLSearchParams.has & delete() 在 Node 中


has() 简介

此函数根据查询参数返回真或假。如果存在参数的名值对,则该函数将返回真。

语法

var bool = URLSearchParams.has(name);

如果存在 name,则将返回 TRUE,否则返回 FALSE。

参数

输入参数是需要在 URL 中搜索的 name。

示例 

// Defining the URL as a constant
const myURL = new URL(
   'https://example.org/?firstName=John');

// Printing whether the argument exists or not
console.log(myURL.searchParams.get('firstName'));

输出

true

示例

// Defining the URL as a constant
const myURL = new URL(
   'https://example.org/?firstName=John');

// Printing whether the argument exists or not
console.log(myURL.searchParams.get('lastName'));

输出

false

delete() 简介

它将删除/移除传入参数的出现。

语法

URLSearchParams.delete(name);

删除传入参数后,它将返回修改后的 URL。

参数

传入需要从 URL 中删除的 name。

示例

// Defining the URL as a constant
const params = new URLSearchParams( 'firstName=John&lastName=Chan');

   console.log(params.toString);
   // Removing the 'firstName' parameter
   params.delete('firstName');
   console.log(params.toString());

输出

firstName=John&lastName=Chan
lastName=Chan

示例(当 arg 不存在时)

// Defining the URL as a constant
const params = new URLSearchParams( 'firstName=John&lastName=Chan');

   console.log(params.toString);
   // Removing the 'firstName' parameter
   params.delete('midName');
   console.log(params.toString());

输出

firstName=John&lastName=Chan
firstName=John&lastName=Chan

更新日期:2021 年 4 月 28 日

200 次观看

职业生涯点燃你的薪火

完成课程,获得认证

立即开始
广告
© . All rights reserved.