Blogger Information
Blog 17
fans 0
comment 0
visits 6187
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
演循环的常用的5种形式、数组与对象的解构赋值
生活需要加油
Original
360 people have browsed it

循环的常用的5种形式、数组与对象的解构赋值

1. 循环的常用的5种形式

  1. console.log("1. while循环:")
  2. const arr = ["唐僧", "孙悟空", "猪八戒", "沙悟净"]
  3. let i = 0
  4. while (i < arr.length) {
  5. console.log(arr[i])
  6. i++
  7. }
  8. console.log("----------------------")
  9. console.log("2. for循环:")
  10. for (i = 0; i < arr.length; i++) {
  11. console.log(arr[i])
  12. }
  13. console.log("----------------------")
  14. console.log("3. for-of 遍历数组")
  15. for (let val of arr) {
  16. console.log(val)
  17. }
  18. console.log("----------------------")
  19. console.log("4. for-in 遍历对象")
  20. let westtral = {
  21. teacher: "唐僧",
  22. fbro: "孙悟空",
  23. sbro: "猪八戒",
  24. tbro: "沙悟净",
  25. }
  26. for (let key in westtral) {
  27. console.log(`${key}=>${westtral[key]}`)
  28. }
  29. // 注意:不能用obj.key,避免出线非法字符
  30. console.log("----------------------")
  31. console.log("5.forEach/map: 遍历“数组“")
  32. arr.forEach(function (item, index, arr) {
  33. console.log(item, index, arr)
  34. })
  35. arr.forEach(function (item) {
  36. console.log(item)
  37. })
  38. console.log(".....................")
  39. console.log("箭头函数:")
  40. arr.forEach(item => console.log(item))
  41. console.log(".....................")
  42. console.log("测试forEach方法有无返回值")
  43. let res = arr.forEach(item => item)
  44. console.log(res)
  45. console.log(".....................")
  46. console.log("测试map方法有无返回值")
  47. res = arr.map(item => item)
  48. console.log(res)
  49. console.log(".....................")
  50. console.log("map方法小应用:")
  51. res = arr
  52. .map(
  53. item => `<li>${item}</li>
  54. `
  55. )
  56. .join("")
  57. res = "<ul>\n" + res + "</ul>"
  58. console.log(res)

运行结果如下:

2. 演示数组与对象的解构赋值

···js
// 数组结构和赋值
const score = [100, 90, 90]
let [a, b, c] = score
console.log(a, b, c)

let [company, site] = [“百度”, “www.baidu.com”]
console.log(company, site)
;[company, site] = [“阿里”, “www.alibaba.com”]
console.log(company, site)
;[company, site, ceo = “不详”] = [“阿里”, “www.alibaba.com”]
console.log(company, site, ceo)
;[company, site, …arr] = [“阿里”, “www.alibaba.com”, “Jack Ma”, 50]
let [boss, age] = arr
console.log(company, site, boss, age)
console.log(“—————————————————————“)
// 对象
// 默认变量名和属性相同
//I 变量与当前作用域中的变量命名冲突时,用别名访问
let { id, fruits: fname } = { id: 1, fruits: “水蜜桃” }
console.log(id, fname)

// 复制对象
let prj = { id: 1, pname: “合肥明悦广场” }
console.log(prj)

let { …obj } = prj
console.log(prj === obj)
// 结构传参

let show = function (prj) {
return id:${prj.id} pname:${prj.pname}
}

prj = { id: 2, pname: “上海中心大厦” }
console.log(show(prj))
```
运行结果如下:

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!