1. 程式人生 > >如何用JS獲取陣列的鍵和值

如何用JS獲取陣列的鍵和值

程式碼實現如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        var person = [
            {fname:"John",lname:"DD",age:25},
            {fname:"Mike",lname:"MM",age:23}
        ];

        for(var i = 0; i < person.length; i++)
        {
            for (x in person[i]) {
                console.log(x + "=" + person[i][x]);
            }
        }
        
    </script>
</body>
</html>