Blogger Information
Blog 16
fans 0
comment 0
visits 5725
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
循环,数组/对象的解构
Sinan学习博客
Original
324 people have browsed it

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

  • while()
  • for
  • for-of
  • for-in
  • forEach() / map()

while循环遍历数组

  1. let arr = [1, 2, 3, 4, 5];
  2. let index = 0;
  3. while (index < arr.length) {//条件:index小于数组的长度
  4. console.log(arr[index]);
  5. index++;
  6. }

for循环遍历数组

  1. for (let i = 0; i < arr.length; i++) {
  2. console.log(arr[i]);
  3. }

for-of循环遍历数组的值,不关心索引

  1. for (let value of arr) {
  2. console.log(value);
  3. }

试着用for-of循环遍历对象

  1. const obj = {
  2. id: 1,
  3. item: "手机",
  4. price: 5000,
  5. };
  6. for (let value of obj) {
  7. console.log(value);
  8. //obj is not iterable
  9. }
  10. //对象不能用for-of遍历

for-in循环遍历对象

  1. const obj = {
  2. id: 1,
  3. item: "手机",
  4. price: 5000,
  5. };
  6. for (let value in obj) {
  7. console.log(value);//for-in遍历对象,得到的是对象的属性,不是值
  8. console.log(obj[key]);//得到对象的值
  9. }

forEach/map用于遍历数组

  • 参数相同,返回值不同
  • forEach 无返回值
  • map 有返回值
  1. //forEach的回调函数有3个参数
  2. //值,索引,数组
  3. arr.forEach(function (item,index,arr) {
  4. console.log(item,index,arr);
  5. });
  6. //通常我们只想得到数组的值可以如下
  7. arr.forEach(function (item) {
  8. console.log(item);
  9. });
  10. //回调函数可以箭头函数简写
  11. arr.forEach((item) => console.log(item));

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

数组

  1. //数组解构赋值
  2. let [item, price] = ["手机", 5000];
  3. console.log(item, price); //手机 5000
  4. //修改值
  5. [item, price] = ["电脑", 15000];
  6. console.log(item, price); //电脑 15000
  7. //当变量多,值少时,应该给变量一个默认值
  8. [item, price, color = "blue"] = ["电脑", 15000];
  9. console.log(item, price, color);
  10. //当变量少,值多时,增加变量 如 ...aaa
  11. [item, price] = ["电脑", 15000, "blue", "17英寸"];
  12. [item, price, ...other] = ["电脑", 15000, "blue", "17英寸"];
  13. console.log(item, price, other);
  14. console.log(item, price, ...other);

对象

  1. //解构对象
  2. //当变量已经定义过,变量名会有冲突,可以使用别名,访问时用别名访问。
  3. let { item: phone, price: jiaGe } = { item: "手机", price: 5000 };
  4. console.log(phone, jiaGe);
  5. //应用场景一:克隆对象
  6. let obj = { id: 1, item: "手机", price: 5000 };
  7. let { ...newObj } = obj;
  8. console.log(newObj);
  9. console.log(newObj.item);
  10. //应用场景二:解构传参
  11. let show = function (obj) {
  12. return `${obj.item} : ${obj.price}`;
  13. };
  14. console.log(show(obj));
  15. //参数简化
  16. show = function ({ item, price }) {
  17. return `${item} : ${price}`;
  18. };
  19. console.log(show(obj));
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