Array.from() 与 Array.reduce()

Array.from()方法就是将一个类数组对象或者可遍历对象转换成一个真正的数组 Array.reduce()方法对累加器和数组中的每个元素 (从左到右)应用一个函数,将其减少为单个值。

Array.from()

// 那么什么是类数组对象呢?所谓类数组对象,最基本的要求就是具有length属性的对象。

// 1、将类数组对象转换为真正数组:

let arrayLike = {
    0: "tom",
    1: "65",
    2: "男",
    3: ["jane", "john", "Mary"],
    length: 4
};
let arr = Array.from(arrayLike);
console.log(arr); // ['tom','65','男',['jane','john','Mary']]

// 那么,如果将上面代码中length属性去掉呢?实践证明,参考答案会是一个长度为0的空数组。

// 这里将代码再改一下,就是具有length属性,但是对象的属性名不再是数字类型的,而是其他字符串型的,代码如下:

let arrayLike = {
    name: "tom",
    age: "65",
    sex: "男",
    friends: ["jane", "john", "Mary"],
    length: 4
};
let arr = Array.from(arrayLike);
console.log(arr); // [ undefined, undefined, undefined, undefined ]

// 会发现结果是长度为4,元素均为undefined的数组

// 由此可见,要将一个类数组对象转换为一个真正的数组,必须具备以下条件:

// 1、该类数组对象必须具有length属性,用于指定数组的长度。如果没有length属性,那么转换后的数组是一个空数组。

// 2、该类数组对象的属性名必须为数值型或字符串型的数字

// ps: 该类数组对象的属性名可以加引号,也可以不加引号

// 2、将Set结构的数据转换为真正的数组:

let arr = [12, 45, 97, 9797, 564, 134, 45642];
let set = new Set(arr);
console.log(Array.from(set)); // [ 12, 45, 97, 9797, 564, 134, 45642 ]

//  Array.from还可以接受第二个参数,作用类似于数组的map方法,用来对每个元素进行处理,将处理后的值放入返回的数组。如下:

let arr = [12, 45, 97, 9797, 564, 134, 45642];
let set = new Set(arr);
console.log(Array.from(set, item => item + 1)); // [ 13, 46, 98, 9798, 565, 135, 45643 ]

// 3、将字符串转换为数组:

let str = "hello world!";
console.log(Array.from(str)); // ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d", "!"]

// 4、Array.from参数是一个真正的数组:

console.log(Array.from([12, 45, 47, 56, 213, 4654, 154]));
// 像这种情况,Array.from会返回一个一模一样的新数组

Array.reduce() 

语法:

array.reduce(function(accumulator, currentValue, currentIndex, array), initialValue);

accumulator:累加器,即函数上一次调用的返回值。第一次的时候为 initialValue || arr[0]

currentValue:数组中函数正在处理的的值。第一次的时候initialValue || arr[1]

currentIndex:数据中正在处理的元素索引,如果提供了 initialValue ,从0开始;否则从1开始

array: 调用 reduce 的数组

initialValue:可选项,累加器的初始值。没有时,累加器第一次的值为currentValue;注意:在对没有设置初始值的空数组调用reduce方法时会报错。
//无初始值
[1, 2, 3, 4].reduce(function(accumulator, currentValue, currentIndex, array) {
    return accumulator + currentValue;
}); // 10
callback accumulator currentValue currentIndex array return value
first call 1(数组第一个元素) 2(数组第二个元素) 1(无初始值为 1) [1, 2, 3, 4] 3
second call 3 3 2 [1, 2, 3, 4] 6
third call 6 4 3 [1, 2, 3, 4] 10
//有初始值
[1, 2, 3, 4].reduce(function(accumulator, currentValue, currentIndex, array) {
    return accumulator + currentValue;
}, 10); // 20
callback accumulator currentValue currentIndex array return value
first call 10(初始值) 1(数组第一个元素) 0(有初始值为 0) [1, 2, 3, 4] 11
second call 11 2 1 [1, 2, 3, 4] 13
third call 13 3 2 [1, 2, 3, 4] 16
fourth call 16 4 3 [1, 2, 3, 4] 20
//1.数组元素求和
[1, 2, 3, 4].reduce((a, b) => a + b); //10

//2.二维数组转化为一维数组
[
    [1, 2],
    [3, 4],
    [5, 6]
]
.reduce((a, b) => a.concat(b), []) //[1, 2, 3, 4, 5, 6]

[
    //3.计算数组中元素出现的次数
    (1, 2, 3, 1, 2, 3, 4)
].reduce((items, item) => {
    if (item in items) {
        items[item]++;
    } else {
        items[item] = 1;
    }
    return items;
}, {}) //{1: 2, 2: 2, 3: 2, 4: 1}

[
    //数组去重①
    (1, 2, 3, 1, 2, 3, 4, 4, 5)
].reduce((init, current) => {
    if (init.length === 0 || init.indexOf(current) === -1) {
        init.push(current);
    }
    return init;
}, []) //[1, 2, 3, 4, 5]
[
    //数组去重②
    (1, 2, 3, 1, 2, 3, 4, 4, 5)
].sort()
    .reduce((init, current) => {
        if (init.length === 0 || init[init.length - 1] !== current) {
            init.push(current);
        }
        return init;
    }, []); //[1, 2, 3, 4, 5]

 

 

相关推荐

  1. ==equals

    2024-04-03 21:20:02       52 阅读
  2. ArrayList LinkedList 的选择应用

    2024-04-03 21:20:02       67 阅读
  3. DecontamSCRUB:安装使用

    2024-04-03 21:20:02       67 阅读
  4. PyTorchTensorFlow的安装介绍

    2024-04-03 21:20:02       50 阅读
  5. vectorlist的区别应用?

    2024-04-03 21:20:02       48 阅读
  6. Word字号磅值行距

    2024-04-03 21:20:02       45 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-03 21:20:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-03 21:20:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-03 21:20:02       87 阅读
  4. Python语言-面向对象

    2024-04-03 21:20:02       96 阅读

热门阅读

  1. Composer 常见错误解决

    2024-04-03 21:20:02       32 阅读
  2. Unity3D 基于ECS的技能冷却系统设计与实现

    2024-04-03 21:20:02       36 阅读
  3. 干掉极域电子教室的方法

    2024-04-03 21:20:02       30 阅读
  4. 谈谈你对 ES6 的理解

    2024-04-03 21:20:02       34 阅读
  5. OpenCV中的模块:三维重建-SFM(2)

    2024-04-03 21:20:02       34 阅读
  6. Linux(centos7)部署hive

    2024-04-03 21:20:02       33 阅读
  7. 湖仓管理系统 Amoro部署

    2024-04-03 21:20:02       33 阅读
  8. torch-v1.3.1-build

    2024-04-03 21:20:02       31 阅读
  9. LeetCode 343. 整数拆分

    2024-04-03 21:20:02       32 阅读
  10. STC8H8K64U 学习笔记 - 矩阵键盘

    2024-04-03 21:20:02       39 阅读