本文共 1772 字,大约阅读时间需要 5 分钟。
方式 能得到 for循环 元素 下标 for...in 下标 foreach 元素 下标 for...of 元素
for..in 属性 下标
for...of 元素 foreach 元素
for...of 键 值 forEach 键 值
var arr = [10, 20, 30, 40, 50]; for (let i = 0; i < arr.length; i++) { document.write("for循环:当前元素为:" + arr[i] + ",下标为:" + i + "
"); } for (const key in arr) { document.write("for...in:下标为:" + key + "
"); } arr.forEach((item, index) => { document.write("forEach循环:当前元素为:" + item + ",下标为:" + index + "
");; }) for (const item of arr) { document.write("for...of循环:当前元素为:" + item + "
");; }
var obj = { name: "周润发", age: "40", sex: "男" } for (const key in obj) { document.write("对象遍历:当前属性为:" + key + ",属性值为:" + obj[key] + "
"); }
var set = new Set([10, 20, 30, 40, 50]); for (const item of set) { document.write("set for...of循环:当前元素为:" + item + "
");; } set.forEach(item => { document.write("set foreach循环:当前元素为:" + item + "
");; });
let map = new Map(); map.set("郑爽", "TMD烦死了") map.set("马老师", "年轻人不讲武德") map.set("浑元形意太极拳掌门", "好自为之") map.set("罗志祥", "今晚九点,多人运动") for (const item of map.keys()) { document.write("map for..of遍历:当前键为:" + item + ",值为:" + map.get(item) + "
"); } map.forEach((value, key) => { document.write("map forEach遍历:当前键为:" + key + ",值为:" + value + "
"); })
转载地址:http://lydyf.baihongyu.com/