javascript中的数组、Map、Set和Iterator
2022/1/9 20:36:47
本文主要是介绍javascript中的数组、Map、Set和Iterator,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
javascript中的数组、Map、Set和Iterator
数组
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script> "use strict" var age = [1,2,3,4,56,5,12,45]; age.forEach(function (value){ console.log(value); }) console.log("======================="); // var var index in object for(var num in age){ if(age.hasOwnProperty(num)){ console.log("存在"); } console.log(age[num]); } </script> </head> <body> </body> </html>
Map
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script> "use strict" //Es6新特性 Map和Set //学生的名字 成绩 var map = new Map([['zhangsan',22],['lisi',80],['wangwu',70],['zjaoliu',40]]); var name = map.get("zhangsan"); console.log(name); map.set('admin',75);//新增 console.log(map.get("admin")); map.delete("zhangsan");//删除 console.log(map); </script> </head> <body> </body> </html>
Set
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script> "use strict" var set = new Set([3,34,22,22,22]); console.log(set); set.add(23); console.log(set); set.add(34);//添加 console.log(set); set.delete(3);//删除 console.log(set); console.log(set.has(34));//是否包含某个元素 </script> </head> <body> </body> </html>
Iterator
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script> "use strict" //es6新特性 var a = [1,2,3,4]; for (let x of a){ console.log(x); } console.log("========================="); var map = new Map([['zhangsan',1],['lisi',2],['wangwu',3]]); for (let x of map){ console.log(x); } console.log("========================="); var set = new Set([1,2,3]); for (let x of set){ console.log(x); } </script> </head> <body> </body> </html>
这篇关于javascript中的数组、Map、Set和Iterator的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-02事件委托学习:从入门到实践
- 2025-01-02手机端网页开发学习:初学者指南
- 2025-01-02网页开发学习:初学者指南
- 2025-01-02移动布局学习:新手必读指南
- 2025-01-02移动网页开发学习:新手入门指南
- 2025-01-02右侧跟随效果学习:轻松掌握网页设计中的跟随效果
- 2025-01-02Web布局入门教程
- 2025-01-02Web网页开发入门教程:从零开始构建你的第一个网页
- 2025-01-024D学习入门教程
- 2025-01-02变形学习:轻松入门的简单教程