Blogger Information
Blog 27
fans 1
comment 2
visits 90524
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
多种数组类型的访问方式和switch演示
          
Original
389 people have browsed it
  1. /* 作业内容:
  2. 1. 实例演示不同的数组类型与访问方式
  3. 2. 实例演示分支的不同类型,注意else的本质
  4. */
  5. // 一、实例演示不同的数组类型与访问方式
  6. // 1.创建有三个元素的数组
  7. let arr1 = ['八戒','悟空','法师']
  8. // 访问:通过下标索引,从0开始
  9. console.log(arr1[0])
  10. console.log(arr1[1])
  11. console.log(arr1[2])
  12. // 2. 多维数组
  13. let listArry=[
  14. ['张三',33,1500],
  15. ['李四',56,5000],
  16. ['曹孟德',65,55000]
  17. ]
  18. // 访问方式:
  19. console.log(listArry[0]) // [ '张三', 33, 1500 ]
  20. console.log(listArry[1]) // ['李四',56,5000]
  21. console.log(listArry[2]) // ['曹孟德',65,55000]
  22. // 遍历原版
  23. // listArry.forEach(function (items){console.log(items)})
  24. // 遍历简写 去除function,只有一个参数时候去掉()括号,去掉return
  25. listArry.forEach(items =>console.log(items))
  26. // 3.对象数组
  27. let StudentsList =[
  28. {name : '张三',age:25,wage:500},
  29. {name : '悟空',age:125,wage:2500},
  30. {name : '刘能',age:85,wage:6500},
  31. ]
  32. StudentsList.forEach(items2 => console.log('对象数组访问方式:'+ items2['name'] +'年龄:'+ items2['age']))
  33. // 4.类数组
  34. let arr2 = {
  35. 0:'八戒',
  36. 1:'悟空',
  37. 2:'法师',
  38. length:3,
  39. }
  40. Array.from(arr2).forEach(items => console.log('类数组访问方式:' + items))
  41. // 二、 实例演示分支的不同类型,注意else的本质
  42. // 单分支
  43. let a = 1+1
  44. if (a != 1) {
  45. console.log('等于2')
  46. }
  47. if (a !=1){
  48. // 执行代码
  49. }else if(a !=2){
  50. console.log('else if 就是 再如果')
  51. } else {
  52. console.log('else 就是 前面都不满足才执行')
  53. }
  54. let number = 6
  55. switch (number) {
  56. case 0:
  57. console.log('数字为0')
  58. break
  59. case 1:
  60. console.log('数字为1')
  61. break
  62. case 2:
  63. console.log('数字为2')
  64. break
  65. case 3:
  66. console.log('数字为3')
  67. break
  68. case 4:
  69. console.log('数字为4')
  70. break
  71. case 5:
  72. console.log('数字为5')
  73. break
  74. default:
  75. console.log('非法输入')
  76. }

运行结果:

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