10 个值得掌握的 reduce 技巧

作为一个前端开发者,一定有接触过 reduce 函数,它是一个强大而实用的数组方法,熟练掌握 reduce 的使用可以在开发中提高开发效率和代码质量。本文介绍的 reduce 的 10 个技巧值得拥有,可以让你少写很多代码!

reduce 方法在数组的每个元素上执行提供的回调函数迭代器。它传入前一个元素计算的返回值,结果是单个值,它是在数组的所有元素上运行迭代器的结果。

迭代器函数逐个遍历数组的元素,在每一步中,迭代器函数将当前数组值添加到上一步的结果中,直到没有更多元素要添加。

语法

参数包含回调函数和可选的初始值,如下:

array.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])

  • callback(必须):执行数组中每个值(如果没有提供 initialValue 则第一个值除外)的 reducer 函数,包含四个参数

    • accumulator(必须):累计器累计回调的返回值; 它是上一次调用回调时返回的累积值,初始值可以通过initialValue定义,默认为数组的第一个元素值,累加器将保留上一个操作的值,就像静态变量一样

    • currentValue(必须):数组中正在处理的元素

    • index(可选):数组中正在处理的当前元素的索引。 如果提供了 initialValue,则起始索引号为 0,否则从索引 1 起始。

      注意:如果没有提供 initialValuereduce 会从索引 1 的地方开始执行 callback 方法,跳过第一个索引。如果提供 initialValue,从索引 0 开始。

    • array(可选):调用 reduce() 的数组

  • initialValue(可选):作为第一次调用 callback 函数时的第一个参数的值。 如果没有提供初始值,则将使用数组中的第一个元素。 在没有初始值的空数组上调用 reduce 将报错

1. 计算数组的最大值和最小值

有很多种方式可以获取数组的最大值或最小值?

使用 Math.max 和 Math.min

使用 Math 的 API 是最简单的方式。

const arrayNumbers = [-1, 10, 6, 5, -3]
const max = Math.max(...arrayNumbers)
const min = Math.min(...arrayNumbers)
console.log(`max=${max}`)
console.log(`min=${min}`)

使用 reduce

一行代码,就可以实现与 Math 的 API 相同的效果。

const arrayNumbers = [-1, 10, 6, 5, -3]
const getMax = (array) => array.reduce((max, num) => (max > num ? max : num))
const getMin = (array) => array.reduce((max, num) => (max < num ? max : num))

const max = getMax(arrayNumbers)
const min = getMin(arrayNumbers)
console.log(`max=${max}`)
console.log(`min=${min}`)

或者写成一个函数:

const arrayNumbers = [-1, 10, 6, 5, -3]

const getMaxOrMin = (array, type = "min") =>
    type === "max"
        ? array.reduce((max, num) => (max > num ? max : num))
        : array.reduce((max, num) => (max < num ? max : num))

const max = getMaxOrMin(arrayNumbers, "max")
const min = getMaxOrMin(arrayNumbers, "min")
console.log(`max=${max}`)
console.log(`min=${min}`)

2. 数组求和和累加器

使用 reduce ,可以轻松实现多个数相加或累加的功能。

const sum = (...nums) => {
    return nums.reduce((sum, num) => sum + num);
};


const accumulator = (...nums) => {
    return nums.reduce((acc, num) => acc * num);
};
const arrayNumbers = [1, 3, 5];

console.log(accumulator(1, 2, 3)); 
console.log(accumulator(...arrayNumbers)); 

console.log(sum(1, 2, 3, 4, 5)); 
console.log(sum(...arrayNumbers)); 

3. 格式化搜索参数

获取 URL 种的搜索参数是经常要处理的功能。

// url https://www.devpoint.cn/index.shtml?name=devpoint&id=100
// 格式化 search parameters
{
    name: "devpoint",
    id: "100",
}

常规方式

这是大多数人使用它的方式。

const parseQuery = (search = window.location.search) => {
    const query = {};
    search
        .slice(1)
        .split("&")
        .forEach((it) => {
            const [key, value] = it.split("=");
            query[key] = decodeURIComponent(value);
        });
    return query;
};
console.log(parseQuery("?name=devpoint&id=100")); 

使用 reduce
const parseQuery = (search = window.location.search) =>
    search
        .replace(/(^\?)|(&$)/g, "")
        .split("&")
        .reduce((query, it) => {
            const [key, value] = it.split("=");
            query[key] = decodeURIComponent(value);
            return query;
        }, {});

console.log(parseQuery("?name=devpoint&id=100")); 

4. 反序列化搜索参数

当要跳转到某个链接并为其添加一些搜索参数时,手动拼接的方式不是很方便。如果要串联的参数很多,那将是一场灾难。

const searchObj = {
    name: "devpoint",
    id: 100,
    // ...
}
const strLink = `https://www.devpoint.cn/index.shtml?name=${searchObj.name}&age=${searchObj.id}`
console.log(strLink)

reduce 可以轻松解决这个问题。

const searchObj = {
    name: "devpoint",
    id: 100,
    
};
const stringifySearch = (search = {}) =>
    Object.entries(search)
        .reduce(
            (t, v) => `${t}${v[0]}=${encodeURIComponent(v[1])}&`,
            Object.keys(search).length ? "?" : ""
        )
        .replace(/&$/, "");

const strLink = `https://www.devpoint.cn/index.shtml${stringifySearch(
    searchObj
)}`;
console.log(strLink); 

5. 展平多层嵌套数组

如何展平多层嵌套数组吗?

const array = [1, [2, [3, [4, [5]]]]]
const flatArray = array.flat(Infinity)

console.log(flatArray)

如果运行环境支持方法 flat ,则可以直接用,如果不支持,使用 reduce 也可以实现和flat一样的功能。

const array = [1, [2, [3, [4, [5]]]]]

const flat = (arrayNumbers) =>
    arrayNumbers.reduce(
        (acc, it) => acc.concat(Array.isArray(it) ? flat(it) : it),
        []
    )
const flatArray = flat(array)

console.log(flatArray)

6. 计算数组成员的数量

如何计算数组中每个成员的个数?即计算重复元素的个数。

const count = (array) =>
    array.reduce(
        (acc, it) => (acc.set(it, (acc.get(it) || 0) + 1), acc),
        new Map()
    );
const array = [1, 2, 1, 2, -1, 0, "0", 10, "10"];
console.log(count(array));

这里使用了数据类型 Map ,关于 JavaScript 的这个数据类型,有兴趣可以阅读下文:

JavaScript 数据结构之 Map[1]

上面代码的输出结果如下:

Map(7) {
  1 => 2,
  2 => 2,
  -1 => 1,
  0 => 1,
  '0' => 1,
  10 => 1,
  '10' => 1
}

7.获取一个对象的多个属性

这是一个项目开发中比较常遇见的场景。通过 API 获取后端数据,前端很多时候只需要取其中部分的数据。

// 一个有很多属性的对象
const obj = {
    a: 1,
    b: 2,
    c: 3,
    d: 4,
    e: 5,
    // ...
};
// 只是想得到它上面的一些属性来创建一个新的对象
const newObj = {
    a: obj.a,
    b: obj.b,
    c: obj.c,
    d: obj.d,
    // ...
};

这个时候可以使用 reduce 来解决。

 *
 * @param {*} obj 原始对象
 * @param {*} keys 需要获取的属性值列表,数组形式
 * @returns
 */
const getObjectKeys = (obj = {}, keys = []) =>
    Object.keys(obj).reduce(
        (acc, key) => (keys.includes(key) && (acc[key] = obj[key]), acc),
        {}
    );

const obj = {
    a: 1,
    b: 2,
    c: 3,
    d: 4,
    e: 5,
    
};
const newObj = getObjectKeys(obj, ["a", "b", "c", "d"]);
console.log(newObj); 

8.反转字符串

反转字符串是面试中最常问到的 JavaScript 问题之一。

const reverseString = (string) => {
    return string.split("").reduceRight((acc, s) => acc + s);
};
const string = "devpoint";
console.log(reverseString(string)); 

9.数组去重

reduce 也很容易实现数组去重。

const array = [1, 2, 1, 2, -1, 10, 11]
const uniqueArray1 = [...new Set(array)]
const uniqueArray2 = array.reduce(
    (acc, it) => (acc.includes(it) ? acc : [...acc, it]),
    []
)

console.log(uniqueArray1)
console.log(uniqueArray2)

10. 模拟方法 flat

虽然现在的JavaScript有原生方法已经实现了对深度嵌套数组进行扁平化的功能,但是如何才能完整的实现扁平化的功能呢?下面就是使用 reduce 来实现其功能:

Array.prototype.flat2 = function (n = 1) {
    const len = this.length;
    let count = 0;
    let current = this;
    if (!len || n === 0) {
        return current;
    }
    
    const hasArray = () => current.some((it) => Array.isArray(it));
    
    while (count++ < n && hasArray()) {
        current = current.reduce((result, it) => result.concat(it), []);
    }
    return current;
};
const array = [1, [2, [3, [4, [5]]]]];

console.log(array.flat()); 
console.log(array.flat2()); 

console.log(array.flat(Infinity)); 
console.log(array.flat2(Infinity)); 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Web面试那些事儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值