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