Blogger Information
Blog 20
fans 0
comment 0
visits 8468
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
演示不同的数组类型与访问方式和分支的不同类型
A 管志岗-电商策划
Original
263 people have browsed it

1. 实例演示不同的数组类型与访问方式

  1. 总共有三个数组类型:
  2. 1. 多维数组
  3. 2. 对象数组
  4. 3. 类数组
  5. // 1. 多维数组
  6. const arr1 = [
  7. [1, '西瓜', 10],
  8. [2, '苹果', 20],
  9. [3, '黄桃', 30],
  10. ]
  11. // arr1.forEach(function (item, key, arr1){
  12. // console.log(item,key,arr1);
  13. // })
  14. // arr1.forEach(function (item){
  15. // console.log(item);
  16. // })
  17. // 用箭头函数缩写一下
  18. // 1. 去掉function, 括号后面增加=>,
  19. // 2. 只有一个语句,可以删除大括号
  20. arr1.forEach ( item => console.log(item))
  21. console.log('-----------------------------');
  22. // 2. 对象数组
  23. // 成员是一个对象字面量,前后端分离开发中,服务器返回JSON
  24. const fruits = [
  25. {id:1, name: '西瓜', price:10},
  26. {id:2, name: '苹果', price:20},
  27. {id:3, name: '黄桃', price:30},
  28. ]
  29. fruits.forEach ( item => console.log(item))
  30. // 3. 类数组
  31. // 不是class, 类:类似,像,类数组-> 类似一个数组,但不是数组
  32. // 仍然是一个对象,用对象来模拟一个数组
  33. // dom编程,浏览器中的对象
  34. const likeArr = {
  35. 0:'admin',
  36. 1:'admin@qq.com',
  37. 2:'21343222',
  38. length:3
  39. }
  40. // 类数组特征:
  41. // 1. 由0开始的递增的正整数的索引/属性
  42. // 2. 必须有'length',表示成员数量/数组长度
  43. // console.log(likeArr);
  44. // likeArr.forEach(item => console.log(item))
  45. // 将类数组转为真正的数组
  46. // console.log(Array.from(likeArr));
  47. // [ 'admin', 'admin@qq.com', '21343222' ]
  48. Array.from(likeArr).forEach(item => console.log(item))
  49. // 4. 函数数组
  50. // const events = [
  51. // function(){
  52. // return '准备发射'
  53. // },
  54. // function(){
  55. // return '击中目标'
  56. // },
  57. // function(){
  58. // return '敌人投降'
  59. // },
  60. // ]
  61. // 箭头函数
  62. const events = [
  63. () => '准备发射',
  64. () => '击中目标',
  65. () => '敌人投降',
  66. ]
  67. // ev 是一个函数,要调用ev()
  68. // events.forEach(events => console.log(events()))
  69. events.forEach(ev => console.log(ev()))
  70. // 5. 对象方法
  71. // 对象的方法中,其实就是属性,只不过他得值是一个匿名函数
  72. const user = {
  73. uname: '管志岗',
  74. email: 'admin@qq.com',
  75. getUser: function() {
  76. // this 当前对象的引用, user
  77. // 模板字符串要用反引号``,可以打出内容,''这个是打印的模板字面量${user.uname}
  78. // return `${user.uname}: (${user.email})`
  79. return `${this.uname}:(${this.email})`
  80. },
  81. }
  82. console.log(user.getUser());
  83. console.log(user.email,user.uname);
  84. console.log('-------------------------');
  85. //用数组调用
  86. const user1 = [
  87. '管志岗',
  88. 'admin@qq.com',
  89. function() {
  90. return `${this[0]}:(${this[1]})`
  91. },
  92. ]
  93. console.log(user1[2]());
  94. console.log('---用数组调用---');
  95. // 数组与对象的区别:
  96. // 数组与对象并无本质区别,仅仅是语法上的不同
  97. // 仅仅是成员声明方式和访问方式不同
  98. // 显然对象的语义化更好
  99. // 所以,可以将数组,看成对象的一个子集或特例
  100. // 函数的本质
  101. // 函数是js中,最重要的数据类型
  102. // 函数可视为一个普通的值
  103. // 函数可以充当返回值,参数等,凡是用到值的地方
  104. // 如果没有return,则返回undefined

2. 实例演示分支的不同类型,注意else的本质

  1. // todo 流程控制:分支
  2. // 1. 顺序: 默认,与源码书写循序一直,不必再浪费时间
  3. // 1. 单分支
  4. // 2. 双分支(简化三元)
  5. // 条件
  6. if (true) {
  7. }
  8. // 1. 单分支
  9. if (true) {
  10. console.log("success");
  11. }
  12. console.log("----------------------");
  13. // 2. 双分支: true/default
  14. if (!true) {
  15. console.log("success");
  16. } else {
  17. console.log("fall");
  18. }
  19. // else : 不一定是 false,只要不满足条件就执行这里
  20. // else :默认分支
  21. // 三元进行简化
  22. let success = false
  23. let result = success ? "成功" : "失败";
  24. // if (status) {
  25. // return "成功";
  26. // } else {
  27. // return "失败";
  28. // }
  29. console.log(result);
  30. console.log("-------------------------");
  31. // 3. 多分支
  32. // if-else 嵌套 用switch来替换更好
  33. grade = "C";
  34. // if (grade == "A") {
  35. // console.log("优秀");
  36. // } else if (grade == "B") {
  37. // console.log("良好");
  38. // } else if (grade == "C") {
  39. // console.log("合格");
  40. // } else if (grade == "D") {
  41. // console.log("补考");
  42. // } else {
  43. // console.log("非法输入");
  44. // }
  45. // grade = "d";
  46. switch (grade) {
  47. case "A":
  48. console.log("优秀");
  49. break;
  50. case "B":
  51. console.log("良好");
  52. break;
  53. case "C":
  54. console.log("合格");
  55. break;
  56. case "D":
  57. console.log("补考");
  58. break;
  59. default:
  60. console.log("非法输入");
  61. }
  62. // 上面是单值,区间判断
  63. let score = 75;
  64. // switch 是true才能进入代码体
  65. switch (true) {
  66. case score >= 80 && score <= 100:
  67. console.log("优秀");
  68. break;
  69. case score >= 60 && score <= 79:
  70. console.log("及格");
  71. break;
  72. case score >= 40 && score <= 59:
  73. console.log("差等");
  74. break;
  75. case score >= 0 && score <= 39:
  76. console.log("补考");
  77. break;
  78. default:
  79. console.log("非法输入");
  80. }
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!