JavaScript array.entries() 方法
entries() 方法创建一个新的数组迭代器对象,用于遍历数组中的每个值的键/值对。 键表示索引号,其值为一个项。 它不会影响原始数组。
语法
以下语法表示 entries() 方法:
array.entries()
参数
它没有任何参数。
返回值
它返回新创建的数组迭代器对象。迭代器对象代表数组的每个元素,并为它们分配键。
JavaScript数组entries()方法示例
我们来实现一些示例,以更好地理解toString()方法:
示例1: 一个简单的数组entries()方法和一个数组。
<html>
<head> <h5> Array Methods </h5>
<body>
    <script>
    var arr=['John','Michael','Embrose','Herry','Lewis'];
    var itr=arr.entries();
    document.write("After applying the entries method:"+"<br>");
    for(var e of itr) //for loop using var.
    {
        document.write(e+"</br>");
    }
    </script>
</body>
</head>
</html>
输出:
输出显示了数组的元素和其对应的键。因此,它们一起形成了一个键值对。

示例2: 这个示例通过let声明表示了数组的entries()方法。
<html>
<head> <h5> Array Methods </h5>
<body>
    <script>
    var arr=['John','Michael','Embrose','Herry','Lewis']; // array elements
    var itr=arr.entries();
    document.write("After applying the entries method:"+"<br>");
    for(let e of itr) // let declares a variable, but its scope is within the block.
    {
        document.write(e+"</br>");
    }
    </script>
</body>
</head>
</html>
输出:
输出结果与上面的示例相同。

示例3: 点击“应用”按钮后,此示例将显示数组的(键,值)对。
<html>
<head> <h5> Array Methods </h5>
<body>
    <script>
        function arr()
        {
               var name=['John','Serious','Hadrik','Harry'];
                var itr=name.entries(); //Using entries() method.
                         document.write("After applying the entries method:"+"<br>");
                 for(x of itr)
            {
            document.write("<br>"+x); //This will display one array                 element per line. 
            }
        }
        </script>
</body>
<input type="button" onClick="arr()" value=" Apply"/> //invoking the arr() function.
</head>
</html>
输出:
调用arr()函数之后,输出如下:

示例4: 在给定数组中显示候选人的位置。
<html>
<head> <h5> Array Methods </h5>
<body>
    <script>
        function position()
        {
            var name=['John','Serious','Hadrik','Harry'];
            var itr=name.entries(); //Using entries() method.
            document.write("The positions of each candidate in a sequence are:  "+"<br>");
            for(x of itr)
            {
                document.write("<br>"+x); //This will display one array element per line with its key. 
            }
        }
        </script>
</body>
<input type="button" onClick="position()" value=" Click to Display"/> 
</head>
</html>
输出:
输出结果将会是:

示例5:
<html>
<head> <h5> Array Methods </h5>
<body>
    <script>
        function position()
        {
            var name=[1,2,3,4,5,6,7,8,8,9];
            var itr=name.entries(); //Using entries() method.
            document.write("The positions of each number in a sequence are:  "+"<br>");
            for(x of itr)
            {
                document.write("<br>"+x); //This will display one array element per line with its key. 
            }
        }
        </script>
</body>
<input type="button" onClick="position()" value=" Click to Display"/> 
</head>
</html>
输出:
单击“显示”按钮后,数组中每个数字的位置将为:

极客笔记