- 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 - 討論
Node.js - Buffer.entries() 方法
NodeJS 的 buffer.entries() 方法根據給定的緩衝區建立一個迭代器物件,並返回緩衝區內容中的一對 [索引,位元組]。
語法
以下是 NodeJS entries() 方法的語法:
buffer.entries()
引數
此方法沒有任何引數。
返回值
buffer.entries() 方法返回一個迭代器物件,該物件包含給定緩衝區內容的索引和位元組對。
返回值是一個迭代器,因此要訪問它,可以使用 for-of() 迴圈。您還可以使用 next() 方法訪問值。
示例
在下面的示例中,當控制檯輸出 i 時,將為您提供字母 H、E、L、L 和 0 的索引和 Unicode 位元組值。
var buffer = Buffer.from('HELLO');
for (var i of buffer.entries()) {
console.log(i);
}
輸出
0 是索引,字元 H 的 Unicode 值為 72,E 的值為 69,索引為 1,L 的值為 76,O 的值為 79。
[ 0, 72 ] [ 1, 69 ] [ 2, 76 ] [ 3, 76 ] [ 4, 79 ]
示例
在此示例中,將使用 next() 方法訪問從 buffer.entries() 返回的迭代器。
var buffer1 = Buffer.from('HELLO');
const bufferiterator = buffer1.entries();
let myitr = bufferiterator.next();
while(!myitr.done){
console.log(myitr.value);
myitr = bufferiterator.next();
}
輸出
透過連續呼叫next()方法直到 done 的值為 true 來迴圈迭代器。done: true 表示我們已到達迭代器的末尾。
[ 0, 72 ] [ 1, 69 ] [ 2, 76 ] [ 3, 76 ] [ 4, 79 ]
示例
我們還可以使用 Buffer.entries() 方法將 buffer1 的內容複製到另一個緩衝區。
const buffer1 = Buffer.from("HELLO");
const buffer2 = Buffer.alloc(buffer1.length);
for (const [index, bytevalue] of buffer1.entries()) {
buffer2[index] = bytevalue;
}
console.log("The string in buffer2 is "+buffer2.toString());
輸出
在上面的示例中,我們使用字串:HELLO 建立了一個緩衝區,為 buffer2 分配了相同長度的位元組。稍後,我們迴圈了 buffer1 的迭代器,並使用示例中所示的索引和值更新了 buffer2。
The string in buffer2 is HELLO
nodejs_buffer_module.htm
廣告
