分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

js中数字的4种遍历方式

发布时间:2023-09-06 01:54责任编辑:傅花花关键词:js遍历
<!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> ?

js中数字的4种遍历方式

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved