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.