
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境搭建
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTFul API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 從表中選擇資料
- Node.js - MySQL Where 條件
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 聯接
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 限制結果數量
- Node.js - MongoDB 聯接
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
NodeJS - urlSearchParams.keys() 方法
NodeJS urlSearchParams.keys() 方法是 URLSearchParams 類的一個方法,它返回一個 ES6 迭代器,允許遍歷每個名稱-值對的所有名稱。
URLSearchParams API 提供了訪問和讀取 URL 查詢的方法。此類也位於全域性物件上。
讓我們考慮一個 YouTube URL(‘https://www.youtube.com/watch?t=RS?f=TS&g=FR’),其中 ‘?’ 後面的部分稱為查詢片段。在此查詢中,(t) 是名稱,(RS) 是值。它們一起形成一個名稱-值對。查詢字串中有三個名稱-值對。因此,如果我們將查詢字串分配給 key() 方法,它將返回一個 ES6 迭代器,用於遍歷每個名稱-值對的名稱。
語法
以下是NodeJS URLSearchParams.keys()方法的語法
URLSearchParams.keys()
引數
此方法不接受任何引數。
返回值
此方法返回一個 ES6 迭代器,用於遍歷每個名稱-值對的名稱。
以下示例演示了 NodeJS URLSearchParams.keys() 方法的使用
示例
如果輸入 URL 字串包含查詢片段,則 NodeJS urlSearchParams.keys() 方法將返回一個迭代器,用於遍歷查詢字串中名稱-值對的名稱。
在以下示例中,我們嘗試從查詢字串的名稱-值對中獲取名稱。
const url = require('node:url'); const MyUrl = new URL('https://tutorialspoint.tw?Monday=1&Thursday=4&Friday=5'); console.log("URL: ", MyUrl.href); const Params = new URLSearchParams('monday=1&thursday=4&friday=5'); console.log("Query string: " + Params); console.log('All the names in the query string are: '); for (const name of Params.keys()) { console.log(name); }
輸出
正如我們在下面的輸出中看到的,NodeJS keys() 方法返回名稱-值對中的所有名稱。
URL: https://tutorialspoint.tw/?Monday=1&Thursday=4&Friday=5 Query string: monday=1&thursday=4&friday=5 All the names in the query string are: monday thursday friday
示例
在以下示例中,我們向輸入查詢字串追加一些名稱-值對。然後我們嘗試從名稱-值對中獲取名稱。
const url = require('node:url'); const Params = new URLSearchParams('Monday=1&Thursday=4&Friday=5'); console.log("Query string: " + Params); Params.append('Saturday', 6); Params.append('Sunday', 7); console.log('All the names in the query string are: '); for (const name of Params.keys()) { console.log(name); }
輸出
執行上述程式後,它將生成以下輸出
Query string: Monday=1&Thursday=4&Friday=5 All the names in the query string are: Monday Thursday Friday Saturday Sunday
nodejs_url_module.htm
廣告