JavaScript:reduce全局函数

2017-05-04  本文已影响109人  勇往直前888

数组是有reduce函数的,求和之类的很方便。一些类数组对象,比如{score1 : 100, score2 : 90, score3 : 95 },不能用reduce函数函数,不是很方便。所以考虑提供一个reduce全局函数。
实现的思路很简单,就是将对象的属性遍历出来,组成一个数组,那么就可以方便地使用reduce函数了

数组的reduce函数

JavaScript数组的高级用法-reduce和reduceRight详解

Array.prototype.reduce()

JavaScript中reduce()方法不完全指南

对象属性的遍历

JavaScript中in操作符(for..in)、Object.keys()和Object.getOwnPropertyNames()的区别

Object.keys()

Object.getOwnPropertyNames()

for...in

reduce全局函数

// 全局函数,区别于数组的reduce()方法
module.exports = function reduceFunction(callback, initialValue, target) {
    // 如果f不是函数,调用数组的reduce会报错
    if ('function' !== typeFunction(callback)) {
        return target;
    }
    const type = typeFunction(target)
    if (type === 'array') {
        return '_' === initialValue           // 判断是否调用init参数
            ? target.reduce(callback)
            : target.reduce(callback, initialValue);
    } else if (type === 'object') {
        // ES7 , chrome浏览器已经可以用
        // const values = Object.values(target);
        const keys = Object.keys(target);
        const values = keys.map(function (key) {
            return target[key];
        })
        return '_' === initialValue           // 判断是否调用init参数
            ? values.reduce(callback)
            : values.reduce(callback, initialValue);
    } else {
        return target;
    }
}

// private
function typeFunction(object) {
    return Object.prototype.toString.call(object).slice(8, -1).toLowerCase();
}
const reduceFunction = require('./reduce_function.js');
const log = console.log;

// 数组累加
const items = [10, 120, 1000];
const add = function(previous, current) { 
    return previous + current; 
};
const max = function(previous, current) { 
    return previous > current
        ? previous
        : current;
};
const multiply = function(previous, current) { 
    return previous * current; 
};
const append = function(previous, current) { 
    return previous + '::' + current; 
};

log(items.reduce(add, 0));                  // 1130
log(items.reduce(max, 0));                  // 1000
log(items.reduce(multiply, 1));             // 1200000
log(items.reduce(append));                  // '10::120::1000'
log(reduceFunction(add, 0, items));         // 1130 
log(reduceFunction(max, 0, items));         // 1000 
log(reduceFunction(multiply, 1, items));    // 1200000 
log(reduceFunction(append, '_', items));    // '10::120::1000'

// 对象属性累加
const scores = {
    score1 : 10,
    score2 : 120,
    score3 : 1000,
}

log(reduceFunction(add, 0, scores));        // 1130
log(reduceFunction(max, 0, scores));        // 1000
log(reduceFunction(multiply, 1, scores));   // 1200000
log(reduceFunction(append, '_', scores));   // '10::120::1000'

// 数组转数字的例子,比如[1,6,8,8,8] => 16888
function addDigitValue(previousValue, currentDigit, currentIndex, array) {
    const exponent = (array.length - 1) - currentIndex;
    const digitValue = currentDigit * Math.pow(10, exponent);
    return previousValue + digitValue;
}

log([1,6,8,8,8].reduce(addDigitValue, 0));    // 16888
log(reduceFunction(addDigitValue, 0, {
    万 : 168, 
    千 : 6, 
    百 : 8,
    十 : 8,
    个 : 8,
 }));                                          // 1686888
上一篇下一篇

猜你喜欢

热点阅读