Home » JavaScript

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.