The Buffer.entries()
method is included in the Buffer
class of Node.js. It creates and provides an iterator of [index, byte]
pairs from the buffer object.
We can use the returned iterator to loop through the buffer object.
Buffer.entries()
This method does not accept any parameters.
This method returns an iterator object that contains [index, byte]
pairs that are created from the contents of the buffer to iterate over the buffer object.
In the example below we have:
buf
from abcdef
, using the Buffer.from()
method.for
loop to iterate over the object returned from the buf.entries()
method.index
and Byte
values to the console, which is obtained from the entry
object.//construcing buffer obectconst buf = Buffer.from('abcdef');//looping through iteratorfor (entry of buf.entries()) {//printing the pairs to console.console.log(`Index: ${entry[0]}, Byte: ${entry[1]}`);}
In the following example, we have:
buf
from abcdef
, using the Buffer.from()
method.buf.entries()
iterator to the variable it
.it
is an iterator, we can access the next()
method on the iterator to get the elements.it.next()
to itr
.while
loop to get all the elements and print them to the console.//construcing buffer obectconst buf = Buffer.from('abcdef');//buffer iteratorconst it = buf.entries();//pointer that pointing to element in iterator using nex()let itr = it.next();//using while loop to access all elementswhile(!itr.done){//printing value to consoleconsole.log(itr.value);//pointing pointer to next elementitr = it.next();}