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.

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



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]), "");

chevron_right


Output:

{"name":"GeeksforGeeks"}

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]);

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.