Skip to content
Related Articles

Related Articles

Improve Article

Node.js | Buffer.entries() Method

  • Last Updated : 14 Jan, 2020

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.

Below examples illustrate the use of Buffer.entries() Method in Node.js:



Example 1:




// Node program to demonstrate the
// Buffer.entries() method
  
// Create a Buffer from Buffer.from() function
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:




// Node program to demonstrate the
// Buffer.entries() method
  
// Create a JSON Object
var a = {
    "name": "GeeksforGeeks"
}
  
// 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":"GeeksforGeeks"}

Example 3:




// Node program to demonstrate the
// Buffer.entries() method
  
// Create an array
var 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :