- 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 條件查詢
- 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.values() 方法
NodeJS urlSearchParams.values() 方法是 URLSearchParams 類的成員方法,它返回一個 ES6 迭代器,允許遍歷每個鍵值對的所有值。
讓我們考慮一個 YouTube URL('https://www.youtube.com/watch?z=HY4&z=NJ7'),其中 '?' 後面的部分稱為查詢片段。在這個查詢中,(z) 是鍵,(HY4) 是值。它們一起構成一個鍵值對。
查詢字串中有兩個鍵值對。因此,如果我們將查詢字串賦值給 values() 方法,它將返回一個遍歷每個鍵值對值的 ES6 迭代器。
URLSearchParams API 提供了訪問和讀寫 URL 查詢的方法。此類也存在於全域性物件上。
語法
以下是NodeJS URLSearchParams.values() 方法的語法
URLSearchParams.values()
引數
此方法不接受任何引數。
返回值
此方法返回一個遍歷每個鍵值對值的 ES6 迭代器。
以下示例演示了 NodeJS URLSearchParams.values() 方法的用法
示例
如果輸入的 URL 字串包含查詢片段,則 NodeJS urlSearchParams.values() 方法將返回一個迭代器,該迭代器遍歷查詢字串中所有鍵值對的值。
在以下示例中,我們嘗試從查詢字串的鍵值對中獲取所有值。
const url = require('node:url');
const MyUrl = new URL('https://tutorialspoint.tw?1=one&3=three&6=six&9=nine');
console.log("URL: ", MyUrl.href);
const Params = new URLSearchParams('1=one&3=three&6=six&9=nine');
console.log("Query string: " + Params);
console.log('All the values in the query string are: ');
for (const value of Params.values()) {
console.log(value);
}
輸出
正如我們在下面的輸出中看到的,NodeJS values() 方法返回鍵值對中的所有值。
URL: https://tutorialspoint.tw/?1=one&3=three&6=six&9=nine Query string: 1=one&3=three&6=six&9=nine All the values in the query string are: one three six nine
示例
在以下示例中,我們向輸入的查詢字串追加一些鍵值對。然後我們嘗試獲取鍵值對的值。
const url = require('node:url');
const Params = new URLSearchParams('1=one&3=three&6=six&9=nine');
console.log("Query string: " + Params);
Params.append(12, 'twelve');
Params.append(15, 'fifteen');
console.log('All the values in the query string are: ');
for (const value of Params.values()) {
console.log(value);
}
輸出
執行上述程式後,將生成以下輸出
Query string: 1=one&3=three&6=six&9=nine All the values in the query string are: one three six nine twelve fifteen
nodejs_url_module.htm
廣告
