Node.js Buffer.entries() Method
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
// Node program to demonstrate the // Buffer.entries() method // Create a Buffer from Buffer.from() function const buf = Buffer.from( 'w3wiki' ); 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
// Node program to demonstrate the // Buffer.entries() method // Create a JSON Object let a = { "name" : "w3wiki" } // Convert to a string a = JSON.stringify(a); // Creating a Buffer const b = Buffer.from(a); for (const pair of b.entries()) process.stdout.write(String.fromCharCode(pair[1]), "" ); |
Output:
{"name":"w3wiki"}
Example 3: The below example illustrates the use of Buffer.entries() Method in Node.js:
javascript
// Node program to demonstrate the // Buffer.entries() method // Create an array let arr = [ true , true , false ]; // Creating a buffer 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
Contact Us