當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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>

輸出:

輸出顯示了分配給它的鍵的數組元素。因此,一起形成一個鍵/值對。

JavaScript Array entries() Method

範例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>

輸出:

輸出將與上述示例中顯示的相同。

JavaScript Array entries() Method

示例3:此示例通過單擊 "apply" 按鈕顯示數組的 (key,value) 對。

<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()函數後,輸出出來了:

JavaScript Array entries() Method

示例 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>

輸出:

輸出將是:

JavaScript Array entries() Method

示例 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>

輸出:

點擊 "display" 按鈕後,每個數字在數組中的位置將是:

JavaScript Array entries() Method



相關用法


注:本文由純淨天空篩選整理自 JavaScript Array entries() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。