遍历大杂烩
发布日期:2021-06-29 02:56:31 浏览次数:4 分类:技术文章

本文共 1772 字,大约阅读时间需要 5 分钟。

目录

数组

方式   		            能得到        for循环              元素    	下标         for...in				 		下标        foreach				 元素   		下标        for...of			 元素

对象

for..in              属性      下标

set集合

for...of                元素       foreach			       元素

map集合

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] + "

"); }

set集合

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 + "

");; });

map集合

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 + "

"); })

转载地址:https://blog.csdn.net/z18237613052/article/details/114108492 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:ES6.class类及构造函数,原型对象prototype,实例对象__proto__属性,面向对象三大特性,(封装,继承,多态)
下一篇:ES6集合,Set集合,Map集合,以及for .... of遍历

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月22日 01时24分27秒