前端学习之路

js中遍历数组和遍历对象

2019-09-30  本文已影响0人  itming

js中遍历数组和遍历对象都可以用for:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>js中遍历对象和数组</title>
</head>
<body>
    <script>
         //遍历对象
    var a={
         name:'zain',
         age:26,
         height:170,
         weight:120,
         job:'web前端工程师'
    };
    for(var i in a){
        console.log(i,a[i]);
    }

    console.log('--------------------------------------------');

    //遍历数组
    var arr=['zain','jack','jhon'];
          for(var i in arr){
             console.log(i,arr[i]);
          }

    </script>
</body>
</html>

控制台输出的是:
name zain
age 26
height 170
weight 120
job web前端工程师


0 zain
1 jack
2 jhon

上一篇下一篇

猜你喜欢

热点阅读