Node.js Buffer.entries() Method
Last Updated :
11 Apr, 2023
The Buffer.entries() method is used to create and return the iterator of [index, byte] pairs from the contents of the buffer.
Syntax:
Buffer.entries()
Parameters: This method does not accept any parameters.
Return value: This method returns an Iterator object of pair in [index, byte] format.
Example 1: The below example illustrates the use of Buffer.entries() Method in Node.js:
javascript
const buf = Buffer.from( 'GeeksforGeeks' );
for (const pair of buf.entries()) {
console.log(pair);
}
|
Output:
[ 0, 71 ]
[ 1, 101 ]
[ 2, 101 ]
[ 3, 107 ]
[ 4, 115 ]
[ 5, 102 ]
[ 6, 111 ]
[ 7, 114 ]
[ 8, 71 ]
[ 9, 101 ]
[ 10, 101 ]
[ 11, 107 ]
[ 12, 115 ]
Example 2: The below example illustrates the use of Buffer.entries() Method in Node.js:
javascript
let a = {
"name" : "GeeksforGeeks"
}
a = JSON.stringify(a);
const b = Buffer.from(a);
for (const pair of b.entries())
process.stdout.write(String.fromCharCode(pair[1]), "" );
|
Output:
{"name":"GeeksforGeeks"}
Example 3: The below example illustrates the use of Buffer.entries() Method in Node.js:
javascript
let arr = [ true , true , false ];
const buf = Buffer.from(arr);
for (const pair of buf.entries()) {
console.log( "index " + pair[0] + ", value " + pair[1]);
}
|
Output:
index 0, value 1
index 1, value 1
index 2, value 0
Reference: https://nodejs.org/docs/latest-v11.x/api/buffer.html#buffer_buf_entries
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...