The array.entries() is an inbuilt function in JavaScript which is used to get a new Array that contains the key and value pairs for each index of an array.
Syntax:
array.entries();
Return value:
It returns an array of index and values of the given array on which array.entries() function is going to work.
Browser Support:
Here 2nd column contain int values that are version of the corresponding browser.
Feature | Basic support |
---|---|
Chrome | 38 |
Edge | Yes |
Firefox | 28 |
Internet Explorer | No |
Opera | 25 |
Safari | 8 |
Android webview | Yes |
Chrome for Android | Yes |
Edge mobile | Yes |
Firefox for Android | 28 |
Opera Android | Yes |
iOS Safari | 8 |
Examples:
Here array.entries() method in javaScript used to find out key and value pairs for each index in any given array.
Let’s see JavaScript program:
var array = ['geeksforgeeks', 'gfg', 'Jhon']; var iterator = array.entries(); // expected output: Array [0, "geeksforgeeks"] console.log(iterator.next().value); // expected output: Array [1, "gfg"] console.log(iterator.next().value); // expected output: Array [2, "Jhon"] console.log(iterator.next().value); |
Output:
> Array [0, "geeksforgeeks"] > Array [1, "gfg"] > Array [2, "Jhon"]
Application:
Whenever we need to get key and value pair for each index in any array thet time we use array.entries() Method
Let’s see JavaScript program:
var array = ['geeksforgeeks', 'gfg', 'Jhon']; var iterator = array.entries(); // printing key and value pair from the given // array using for loop. for (let e of iterator) { console.log(e); } |
Output:
> Array [0, "geeksforgeeks"] > Array [1, "gfg"] > Array [2, "Jhon"]
leave a comment
0 Comments