• 把JS中的map方法玩出花来


    一  map是什么

    map(callbackFn)
    map(callbackFn, thisArg)

    map() 方法是一个迭代方法。它为数组中的每个元素调用一次提供的 callbackFn 函数,并用结果构建一个新数组。

    参数

    callbackFn

    数组中的每个元素执行的函数。它的返回值作为一个元素被添加为新数组中。该函数被调用时将传入以下参数:

    element

    数组中当前正在处理的元素。

    index  可选

    正在处理的元素在数组中的索引。

    arrary  可选

    调用了 map() 的数组本身。

    thisArg 可选

    执行 callbackFn 时用作 this 的值。参见迭代方法

    返回值

    一个新数组,每个元素都是回调函数的返回值。

    示例

    1.下面的代码创建了一个新数组,值为原数组中对应数字的平方根。
    1. const numbers = [1, 4, 9];
    2. const roots = numbers.map((num) => Math.sqrt(num));
    3. // roots 现在是 [1, 2, 3]
    4. // numbers 依旧是 [1, 4, 9]
    2.以下代码使用一个包含对象的数组来重新创建一个格式化后的数组。

    (1).

    1. const arrayUsers = [
    2. {
    3. id: 1,
    4. username: "Magic",
    5. address: "Johnson",
    6. },
    7. {
    8. id: 2,
    9. username: "Kobe",
    10. address: "Bryant",
    11. },
    12. {
    13. id: 3,
    14. username: "Lebron",
    15. address: "James",
    16. },
    17. {
    18. id: 4,
    19. username: "Kareem",
    20. address: "Abdul-Jabbar",
    21. },
    22. {
    23. id: 5,
    24. username: "Shaquille",
    25. address: "O’Neal",
    26. },
    27. ];
    28. const newUsers = arrayUsers.map((item) => item.username);
    29. console.log(arrayUsers);
    30. // [
    31. // { id: 1, username: 'Magic', address: 'Johnson' },
    32. // { id: 2, username: 'Kobe', address: 'Bryant' },
    33. // { id: 3, username: 'Lebron', address: 'James' },
    34. // { id: 4, username: 'Kareem', address: 'Abdul-Jabbar' },
    35. // { id: 5, username: 'Shaquille', address: 'O’Neal' }
    36. // ]
    37. console.log(newUsers); // [ 'Magic', 'Kobe', 'Lebron', 'Kareem', 'Shaquille' ]

    (2). 

    1. const kvArray = [
    2. { key: 1, value: 10 },
    3. { key: 2, value: 20 },
    4. { key: 3, value: 30 },
    5. ];
    6. const reformattedArray = kvArray.map(({ key, value }) => ({ [key]: value }));
    7. console.log(reformattedArray); // [{ 1: 10 }, { 2: 20 }, { 3: 30 }]
    8. console.log(kvArray);
    9. // [
    10. // { key: 1, value: 10 },
    11. // { key: 2, value: 20 },
    12. // { key: 3, value: 30 }
    13. // ]
    3. 回调数组中的部分素
    1. const arrayNumbers = [1, 2, 3, 4];
    2. const doubles = (nums) => nums.map((num) => (num % 2 === 1 ? num * 2 : num));
    3. console.log(arrayNumbers); // [ 1, 2, 3, 4 ]
    4. console.log(doubles(arrayNumbers)); // [ 2, 2, 6, 4 ]
    4.在非数组上使用map
    1. const arrayLike = {
    2. length: 3,
    3. 0: 2,
    4. 1: 3,
    5. 2: 4,
    6. };
    7. console.log(Array.prototype.map.call(arrayLike, (x) => x ** 2));
    8. // [ 4, 9, 16 ]

    map() 方法读取 this 的 length 属性,然后访问每个整数索引。不能直接使用map()方法。

    5.将字符串转换为数组。
    1. const language = "China";
    2. const map = Array.prototype.map;
    3. const letters = map.call(language, (eachLetter) => `${eachLetter}`);
    4. console.log(letters); // [ 'C', 'h', 'i', 'n', 'a' ]

    与上例子还是有点区别的。

    6.数组与对象数组

    (1).

    1. let myperson= ['xiaoli','xiaona','xiaoyu','xiaozhu']
    2. let newArr = myperson.map((name) => ({name:name}))
    3. console.log(newArr);
    4. (4) [{…}, {…}, {…}, {…}]
    5. 0:
    6. {name: 'xiaoli'}
    7. 1:
    8. {name: 'xiaona'}
    9. 2:
    10. {name: 'xiaoyu'}
    11. 3:
    12. {name: 'xiaozhu'}
    13. length:
    14. 4
    15. [[Prototype]]:
    16. Array(0)
    17. [[Prototype]]:
    18. Object
    1. let myage = [18,28,38,19]
    2. let myperson= ['xiaoli','xiaona','xiaoyu','xiaozhu']
    3. let newArr = myage.map((age, i) => ({age, name: myperson[i]}))
    4. console.log(newArr);
    5. (4) [{…}, {…}, {…}, {…}]
    6. xiaoyu.js:4
    7. 0:
    8. {name: 'xiaoli', age: 18}
    9. 1:
    10. {name: 'xiaona', age: 28}
    11. 2:
    12. {name: 'xiaoyu', age: 38}
    13. 3:
    14. {name: 'xiaozhu', age: 19}
    15. length:
    16. 4
    myperson.map((name,index) => ({name:name,age:myage[index]}))

    这样更好理解给些,ES6语法

    7.与parseInt()

    1. const returnInt = (element) => parseInt(element, 10);
    2. let arr1=["1", "2", "3"].map(returnInt); // [1, 2, 3]
    3. // 实际结果是一个数字数组(如预期)
    4. // 与上面相同,但使用简洁的箭头函数语法
    5. let arr2=["1", "2", "3"].map((str) => parseInt(str)); // [1, 2, 3]
    6. // 但与 parseInt() 不同,Number() 还会返回一个浮点数或(解析)指数表示法:
    7. let arr4=["1.1", "2.2e2", "3e300"].map(Number); // [1.1, 220, 3e+300]
    8. // 为了进行比较,如果我们对上面的数组使用 parseInt():
    9. let arr5=["1.1", "2.2e2", "3e300"].map((str) => parseInt(str)); // [1, 2, 3]
    10. console.log(arr1);
    11. console.log(arr2);
    12. console.log(arr4);
    13. console.log(arr5);
    14. (3) [1, 2, 3]
    15. (3) [1, 2, 3]
    16. (3) [1.1, 220, 3e+300]
    17. (3) [1, 2, 3]

    有时间继续学习。

  • 相关阅读:
    多线程难点
    JAVA茶叶销售网站计算机毕业设计Mybatis+系统+数据库+调试部署
    Linux之httpd及虚拟主机的配置及使用
    使用pytorch搭建MobileNetV2并基于迁移学习训练
    安徽校园招聘黄金时间
    虹科教您 | 可实现带宽计量和延迟计算的时间敏感网络测试工具RELY-TSN-LAB操作指南与基本功能测试
    Vulnhub靶场之matrix-breakout-2-morpheus
    好教程推荐系列:收录常见的Qt面试题
    InternalResourceViewResolver类简介说明
    竞赛 基于深度学习的视频多目标跟踪实现
  • 原文地址:https://blog.csdn.net/weixin_31707323/article/details/133974871