博客列表 >JS基本语法4:JS循环的5种形式/函数的参数和返回值/数组与对象的解构赋值

JS基本语法4:JS循环的5种形式/函数的参数和返回值/数组与对象的解构赋值

茂林
茂林原创
2024年01月07日 16:28:15895浏览

循环的5种形式

1.while
2.for
3.for..of
4.for…in
5.forEach()/map()

循环的三要素
1.循环入口
2.条件:true开始,false结束
3.更新条件,避免死循环
循环用于数组和对象

1.while

  1. arr = [1, "php", "朱老师", 30];
  2. console.log(arr.length);
  3. //初始化循环入口
  4. let num = 0;
  5. //循环条件:变量num 只要小于数组长度,执行{语句块}
  6. while (num < arr.length) {
  7. console.log(`这是数组元素arr[${num}]的值:` + arr[`${num}`]);
  8. //条件更新
  9. num++;
  10. }

while

2.for
while 循环的语法糖

  1. arr = [1, "php", "朱老师", 30];
  2. for (let num = 0; num < arr.length; num++) {
  3. console.log(`这是数组元素arr[${num}]的值:` + arr[`${num}`]);
  4. }

for
3.for of:循环 遍历“数组”
大多数场景下,我们只关注值,忽略索引

  1. arr = [1, "php", "朱老师", 30];
  2. for (let index of arr) {
  3. // 1. index = arr[0], 输出 value
  4. // 2. index = arr[1], 输出 value
  5. // 3. index = arr[2], 输出 value
  6. // 4.index = arr[3], false / end
  7. // index: 循环变量,用于接收每一次的要被遍历的值
  8. console.log(index);
  9. }

for-of
4.for...in
用于遍历 “对象”
无法直接使用 ‘对象名.属性名 ’获取属性值,要使用‘对象名[索引]’才能获取属性值

  1. teacher = {
  2. id: 1,
  3. name: "朱老师",
  4. cls: "php",
  5. day: 30,
  6. "e mail": "151@qq.com",
  7. };
  8. for (let no in teacher) {
  9. // console.log(no);
  10. //console.log(teacher[no]);
  11. //无法直接使用 ‘对象名.属性名 ’获取属性值,要使用‘对象名[索引]’才能获取属性值
  12. console.log(`${no}=>${teacher[no]}`);
  13. }

for-in
5.forEach / map
这两个函数都是用于遍历 ‘数组’
区别在于 forEach没有返回值,map有返回值
forEach是一个回调函数
forEach函数用法:数组名.forEach(function (值,索引,数组名称))
forEach回调函数的参数,函数值必选,索引和数组名称可选

  1. arr = [1, "php", "朱老师", 30];
  2. arr.forEach(function (items, index, arr) {
  3. console.log(items, index, arr);
  4. });
  5. arr.forEach(function (items) {
  6. console.log(items);
  7. });
  8. //回调函数一般会用箭头函数来代替
  9. res = arr.map(function (item) {
  10. return item;
  11. });
  12. //使用箭头函数得到相同的结果
  13. //使用map函数有返回值,返回的值为一个数组
  14. res = arr.map((item) => item);
  15. console.log(res);

forEach/map

map函数的应用场景
arr.join:将数组的所有元素添加到字符串中,以指定的分隔符字符串分隔。

  1. arr = [1, "php", "朱老师", 30];
  2. // res = arr.map((item) => item);
  3. res = arr.map((item) => `<li>${item}</li>\n`).join("");
  4. res = "<ul>\n" + `${res}` + "</ul>";
  5. console.log(res);

map应用场景

函数的参数与返回值

函数参数

//1.参数不足时,使用默认参数

/ let num = (a, b = 0) => a + b;
console.log(num(3));
/

//2.参数过多时,使用剩余参数
*剩余参数:…数组变量(…rest)
将多余的参数打包在rest数组中

  • 剩余参数的时候,需要将剩下参数解包有三个方法
    1.将剩余参数逐个放入变量中
    2…rest,还可以将数组展开
    2.1压入:作为函数参数的时候
    2.2展开:不作为函数参数的时候
    3.数组解构:将集合中的每一个成员,赋值给一个个独立的变量
    //函数的参数 与 返回值

1.参数不足时,使用默认参数

  1. let num = (a, b = 0) => a + b;
  2. console.log(num(3));

2.参数过多时,使用剩余参数

  1. 剩余参数:...数组变量(...rest)
  2. 将多余的参数打包在rest数组中

将剩余参数的解包有三个方法
1.将剩余参数逐个放入变量中
2…rest,还可以将数组展开
2.1压入:作为函数参数的时候
2.2展开:不作为函数参数的时候
3.数组解构:将集合中的每一个成员,赋值给一个个独立的变量

  1. let num = function (a, b, ...rest) {
  2. console.log(a, b, rest);
  3. };
  4. console.log(num(1, 2, 3, 4, 5, 6));

剩余参数

将剩余参数解包有三个方法
1.将它们逐个放入变量中

  1. let num = function (a, b, ...rest) {
  2. // console.log(a, b, rest);
  3. let c = rest[0];
  4. let d = rest[1];
  5. let e = rest[2];
  6. let f = rest[3];
  7. return a + b + c + d + e + f;
  8. };
  9. console.log(num(1, 2, 3, 4, 5, 6));

剩余参数解包

2….rest,还可以将数组展开

  1. let num = function (a, b, ...rest) {
  2. // console.log(a, b, rest);
  3. /* let c = rest[0];
  4. let d = rest[1];
  5. let e = rest[2];
  6. let f = rest[3];
  7. return a + b + c + d + e + f;*/
  8. let tmp = [a, b, ...rest];
  9. //得到一个新的数组
  10. let sum = 0; //累加器
  11. for (let a = 0; a < tmp.length; a++) {
  12. sum += tmp[a];
  13. }
  14. return sum;
  15. };
  16. console.log(num(1, 2, 3, 4, 5, 6));

剩余参数解包2

3.数组解构:将集合中的每一个成员,赋值给一个个独立的变量

  1. let num = function (a, b, ...rest) {
  2. let [c, d, e, f] = rest;
  3. //将集合中的每一个成员,赋值给一个个独立的变量
  4. let data = [a, b, c, d, e, f];
  5. //迭代累加返回
  6. return data.reduce((prev, curr) => prev + curr, 0);
  7. };
  8. console.log(num(1, 2, 3, 4, 5, 6));

剩余参数解包3

函数的返回值

1.默认单值返回
2.多值返回(数组,对象)

多值返回

  1. const items = [
  2. { id: 1, name: '西瓜', price: 20 },
  3. { id: 2, name: '苹果', price: 30 },
  4. { id: 3, name: '橘子', price: 40 },
  5. { id: 4, name: '香蕉', price: 50 },
  6. { id: 5, name: '樱桃', price: 60 },
  7. ]
  8. // 根据输入的商品id,返回指定的商品集合
  9. // 1,2,返回西瓜,苹果
  10. // 声明函数
  11. const getFruits = function (items, ...nums) {
  12. // console.log(nums)
  13. // 结果返回的数组
  14. let res = []
  15. // 循环nums
  16. // 拿到要获取的id,去遍历items数组,将满足条件的成员,添加到res数组中
  17. for (let i = 0; i < nums.length; i++) {
  18. // 根据id,遍历当前商品集合
  19. items.forEach(function (item) {
  20. if (item.id === nums[i]) {
  21. // push(x):向数组的尾部追加内容
  22. res.push(item)
  23. }
  24. })
  25. }
  26. return res
  27. }
  28. // 调用
  29. let result = getFruits(items, 1, 3, 5)
  30. console.table(result)

函数的解构与赋值

可以将数组中的值或对象的属性取出,赋值给其他变量。
语法:
左边: 模板, 数组用 […], 对象用 {…}
右边: 值(数组,对象)
数组解构

  1. /**
  2. 当变量数量 > 值的数量 时,变量使用默认值
  3. 当变量数量 < 值的数量 时,使用剩余参数 ...rest
  4. */
  5. const x = [1, 2, 3, 4, 5];
  6. const [y, z] = x;
  7. console.log(y); // 1
  8. console.log(z); // 2

对象解构

  1. const obj = { a: 1, b: 2 };
  2. const { a, b } = obj;
  3. // 等同于下面的语句
  4. // const a = obj.a;
  5. // const b = obj.b;
  6. // 默认变量名和属性相同
  7. // let { id, username } = { id: 1, username: '朱老师' }
  8. // 变量与当前作用域中的变量命名冲突时,用别名访问
  9. let { id, uname: username } = { id: 1, uname: '朱老师' }
  10. console.log(id, username)

对象解构应用场景:
1.克隆对象

  1. let obj = { id: 1, uname: "朱老师" };
  2. let { ...obj1 } = obj;
  3. console.log(obj1); // { id: 1, uname: '朱老师' }
  4. console.log(obj1 === obj); //false

2.解构传参

  1. let show = function (user) {
  2. // user 是 object
  3. return `${user.uname}: ( ${user.email} )`
  4. }
  5. user = { uname: '张老师', email: 'zhang@php.cn' }
  6. console.log(show(user))
  7. // 使用对象解构来简化传参
  8. show = function ({ uname, email }) {
  9. return `${uname}: ( ${email} )`
  10. }
  11. console.log(show(user))
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议