js中数字的4种遍历方式

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>数组的遍历方式</title>
    <script type="text/javascript">
        var arr = [11,22,33,55];
        //普通的循环遍历方式
        function first(){  

            for(var i= 0;i<arr.length;i++){
                console.log("第一种遍历方式\t"+arr[i]);
            }
            console.log("111111111111111111111111111111");  

        }
        //2、for ..in 遍历方式
        function second(){
            // for in 遍历需要两个形参 ,index表示数组的下标(可以自定义),arr表示要遍历的数组
            for(var index in arr){
                console.log("第二种遍历方式\t"+arr[index]);  

            }
            console.log("222222222222222222222222222222");
        }  

        //3,很鸡肋的遍历方式
        function third(){
            //第一个参数为数组的元素,第二个元素为数组的下标
            arr.forEach(function(ele,index){
                console.log("第三种遍历方式\t"+arr[index]+"-----"+ele);
            });
            console.log("333333333333333333333333333333");  

        }
        //4,for-of遍历方式
        function forth(){
            //第一个变量ele代表数组的元素(可以自定义) arr为数组(数据源)
            for(var ele of arr){
                console.log("第四种遍历方式\t"+ele);
            }
            console.log("444444444444444444444444444444");
        }
    </script>
</head>
<body>
    <input type="button" value="第一种遍历方式" name="aa" onclick="first();"/><br/>
    <input type="button" value="第二种遍历方式" name="aa" onclick="second();"/><br/>
    <input type="button" value="第三种遍历方式" name="aa" onclick="third();"/><br/>
    <input type="button" value="第四种遍历方式" name="aa" onclick="forth();"/><br/>
</body>
</html>  

原文地址:https://www.cnblogs.com/zhangzonghua/p/9031731.html

时间: 2024-07-31 13:43:19

js中数字的4种遍历方式的相关文章

【学习笔记】——原生js中常用的四种循环方式

一.引言 本文主要是利用一个例子,讲一下原生js中常用的四种循环方式的使用与区别: 实现效果: 在网页中弹出框输入0   网页输出"欢迎下次光临" 在网页中弹出框输入1   网页输出"查询中--" 在网页中弹出框输入2   网页输出"取款中--" 在网页中弹出框输入3   网页输出"转账进行中--" 在网页中弹出框输入其他字符   网页输出"无效按键" 四种循环: for循环 while循环 for  in

【JS】Js中函数的三种调用方式

在同一个页面中,函数名必须是唯一的,并且区分大小写.Js中可以通过下面三种方式调用函数: 1. 函数的简单调用 2. 在事件的响应中调用函数 3. 通过连接调用函数 具体是怎么操作的呢,下面一一讲解: 函数的简单调用:函数的定义语句通畅被放在HTML文件的<head>内,而函数的调用语句通常被放在<body>中.如果函数定义之前调用函数,执行将会出错. 语法如下: <head> <script type="text/javascript">

node.js中函数的两种封装方式

1.创建一js文件(funs.js)function  controller(req,res){          //res.write("发送");          call('hello',req,res);          res.end("");      }module.exports  =  controller;    //此文件中只有一个函数被发布 其他文件中调用:require('./models/funs.js'); controller(

lua中for循环的四种遍历方式

lua中for的四种遍历方式区别 table.maxn 取最大的整数key #table 从1开始的顺序整数最大值,如1,2,3,6 #table == 3 key,value pairs 取每一个键值对 ipairs 取从key==1开始的顺序整数最大值,每个键值对 参考http://rangercyh.blog.51cto.com/1444712/1032925 不过有一个问题, tbtest = { [1] = 1, [2] = 2, [4] = 4, } print(#(tbtest))

List集合中两种遍历方式

遍历List集合中的元素的方法有两种: 第一种:利用迭代器遍历 代码1: // 迭代器 Iterator it=list.iterator(); while(it.hasNext()) { System.out.println(it.next()); } 或者代码2: for(Iterator it=list.iterator();it.hasNext();) { System.out.println(it.next()); }// 与while循环相比优点:对象it在循环结束后,变为垃圾,自动

js数组的4种遍历方式

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"/> 5 <title>数组的遍历方式</title> 6 <script type="text/javascript"> 7 var arr = [11, 22, 33, 55]; 8 //普通的循环遍历方式 9 function first() { 10 11 f

JS几种遍历方式比较

几种遍历方式比较 for of 循环不仅支持数组.大多数伪数组对象,也支持字符串遍历,此外还支持 Map 和 Set 对象遍历. for in 循环可以遍历字符串.对象.数组,不能遍历 Set/Map forEach 循环不能遍历字符串.对象,可以遍历 Set/Map 原文地址:https://www.cnblogs.com/mahmud/p/10290150.html

java中ArrayList集合的三种遍历方式

public class ListDemo { public static void main(String[] args) { ArrayList<String> mList = new ArrayList<>(); mList.add("郭靖"); mList.add("黄蓉"); mList.add("洪七公"); mList.add("周伯通"); // 第一种遍历方式:普通for循环 for

js中继承的几种用法总结(apply,call,prototype)

本篇文章主要介绍了js中继承的几种用法总结(apply,call,prototype) 需要的朋友可以过来参考下,希望对大家有所帮助 一,js中对象继承 js中有三种继承方式 1.js原型(prototype)实现继承 <SPAN style="<SPAN style="FONT-SIZE: 18px"><html>   <body>  <script type="text/javascript">