Array entries() method with example in JavaScript

JavaScript entries() method: Here, we are going to learn about the entries() method of array in JavaScript.
Submitted by IncludeHelp, on March 02, 2019

JavaScript entries() method

entries() method is used to create an iterator object of an array to access the keys (index) and values.

Syntax:

    array.entries();

Parameters: None

Return value: An iterator object of the array.

Note: To print the key, value using iterator, we use iterator.next().value.

Example:

    Input:
    var names = ["Manju", "Amit", "Abhi", "Radib", "Prem"];

    Function call:
    var it = names.entries();
    it.next().value;
    
    Output:
    0,Manju

JavaScript Code to demonstrate example of Array.entries() method

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>
		var names = ["Manju", "Amit", "Abhi", "Radib", "Prem"];
		
		//creating an iterator 
		var it = names.entries();
		
		//printing keys(index) & values 
		document.write(it.next().value + "<br>");
		document.write(it.next().value + "<br>");
		document.write(it.next().value + "<br>");
		document.write(it.next().value + "<br>");
		document.write(it.next().value + "<br>");		
	</script>
</body>
</html>

Output

0,Manju
1,Amit
2,Abhi
3,Radib
4,Prem

JavaScript Array Object Methods »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.