• Node.js Video Tutorials

NodeJS - URLSearchParams.delete() 方法



NodeJS URLSearchParams.delete() 方法是 URLSearchParams 類的一個方法,它接受一個名稱作為引數,並刪除查詢字串中所有名稱與該名稱匹配的名稱/值對。

URL 的查詢部分可以使用 URLSearchParams API 進行讀取和寫入。此類也可在全域性物件上使用。

語法

以下是 NodeJS URLSearchParams.delete() 方法的語法

URLSearchParams.delete(name)

引數

  • name: 此引數指定要刪除的名稱。

返回值

此方法刪除所有名稱為 name 的名稱/值對。

示例

如果我們將名稱傳遞給 NodeJS URLSearchParams.delete() 方法,它將從輸入查詢字串中刪除該名稱/值對。

在以下示例中,我們嘗試透過將名稱傳遞給 NodeJS delete() 方法來從查詢字串中刪除一個名稱/值對。

const url = require('node:url');

let Myurl = new URL('https://tutorialspoint.tw?AWS=1499&DEVOPS=1999&Cloud=2499');
console.log("URL: ", Myurl.href); If we pass a key/value pair to the append() method, it will add them at the end of the query string.

let params = new URLSearchParams('AWS=1499&DEVOPS=1999&Cloud=2499');
console.log("Query portion of the URL: " + params.toString());

//deletes 'DEVOPS' from the query string.
params.delete('DEVOPS');
//The final Query string is: 'AWS=1499&Cloud=2499'
console.log("After deleting 'DEVOPS': " + params.toString());

輸出

從下面的輸出中可以看到,名稱為 'DEVOPS' 的名稱/值對已被刪除。

URL:  https://tutorialspoint.tw/?AWS=1499&DEVOPS=1999&Cloud=2499
Query portion of the URL: AWS=1499&DEVOPS=1999&Cloud=2499
After deleting 'DEVOPS': AWS=1499&Cloud=2499

示例

delete() 方法將刪除查詢字串中所有名稱與要刪除的名稱匹配的名稱/值對。

在以下示例中,我們嘗試刪除名稱為 'DEVOPS' 的名稱/值對。

const url = require('node:url');

let Myurl = new URL('https://tutorialspoint.tw?AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999');
console.log("URL: ", Myurl.href);

let params = new URLSearchParams('AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999');
console.log("Query portion of the URL: " + params.toString());

//deletes every pair with name 'DEVOPS' from the query string.
params.delete('DEVOPS');
//The final Query string is: 'AWS=1499&Cloud=2499'
console.log("After deleting 'DEVOPS': " + params.toString());

輸出

從下面的輸出中可以看到,查詢字串中所有 'DEVOPS' 的出現都被刪除了。

URL:  https://tutorialspoint.tw/?AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999
Query portion of the URL: AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999
After deleting 'DEVOPS': AWS=1499&Cloud=2499

示例

如果要刪除的名稱不在輸入查詢字串中,則查詢字串將保持不變。

在以下示例中,我們嘗試刪除不在查詢字串中的名稱。

const url = require('node:url');

let Myurl = new URL('https://tutorialspoint.tw?AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999');
console.log("URL: ", Myurl.href);

let params = new URLSearchParams('AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999');
console.log("Query portion of the URL: " + params.toString());

//deletes every pair with name 'GCP' from the query string.
params.delete('GCP');
//The final Query string is: 'AWS=1499&Cloud=2499'
console.log("After deleting 'GCP': " + params.toString());

輸出

從輸出中可以看到,輸入查詢字串保持不變。

URL:  https://tutorialspoint.tw/?AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999
Query portion of the URL: AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999
After deleting 'GCP': AWS=1499&DEVOPS=1999&Cloud=2499&DEVOPS=4999&DEVOPS=5999
nodejs_url_module.htm
廣告