当前位置:首页 > JavaScript

sumif实现js

2026-03-02 04:22:17JavaScript

实现 JavaScript 中的 SUMIF 功能

在 JavaScript 中实现类似 Excel 的 SUMIF 功能,可以通过自定义函数来完成。SUMIF 的核心逻辑是根据条件筛选数据,并对符合条件的值求和。

基础实现方法

定义一个函数,接受三个参数:范围数组、条件、求和数组(可选)。如果求和数组未提供,则默认对范围数组求和。

sumif实现js

function sumif(range, condition, sumRange) {
    let sum = 0;
    const sumArray = sumRange || range;

    for (let i = 0; i < range.length; i++) {
        if (typeof condition === 'function') {
            if (condition(range[i])) {
                sum += sumArray[i];
            }
        } else {
            if (range[i] === condition) {
                sum += sumArray[i];
            }
        }
    }

    return sum;
}

使用示例

对数组 [1, 2, 3, 4, 5] 中大于 2 的值求和:

const data = [1, 2, 3, 4, 5];
const result = sumif(data, (x) => x > 2);
console.log(result); // 输出 12 (3 + 4 + 5)

对特定值求和:

sumif实现js

const result = sumif(data, 3);
console.log(result); // 输出 3

高级实现支持多种条件

扩展函数以支持字符串条件(如 ">2"):

function sumif(range, condition, sumRange) {
    let sum = 0;
    const sumArray = sumRange || range;

    for (let i = 0; i < range.length; i++) {
        let match = false;

        if (typeof condition === 'function') {
            match = condition(range[i]);
        } else if (typeof condition === 'string') {
            const operator = condition.match(/^([<>=!]+)/)?.[0];
            const value = parseFloat(condition.replace(/^[<>=!]+/, ''));

            if (operator === '>') match = range[i] > value;
            else if (operator === '<') match = range[i] < value;
            else if (operator === '>=') match = range[i] >= value;
            else if (operator === '<=') match = range[i] <= value;
            else if (operator === '!=') match = range[i] != value;
            else match = range[i] == condition;
        } else {
            match = range[i] == condition;
        }

        if (match) sum += sumArray[i];
    }

    return sum;
}

使用字符串条件示例

const result = sumif(data, ">2");
console.log(result); // 输出 12

对对象数组求和

处理对象数组时,可以指定属性名:

function sumifObj(arr, property, condition, sumProperty) {
    let sum = 0;
    const sumProp = sumProperty || property;

    for (const item of arr) {
        let match = false;

        if (typeof condition === 'function') {
            match = condition(item[property]);
        } else if (typeof condition === 'string') {
            const operator = condition.match(/^([<>=!]+)/)?.[0];
            const value = parseFloat(condition.replace(/^[<>=!]+/, ''));

            if (operator === '>') match = item[property] > value;
            else if (operator === '<') match = item[property] < value;
            else if (operator === '>=') match = item[property] >= value;
            else if (operator === '<=') match = item[property] <= value;
            else if (operator === '!=') match = item[property] != value;
            else match = item[property] == condition;
        } else {
            match = item[property] == condition;
        }

        if (match) sum += item[sumProp];
    }

    return sum;
}

对象数组使用示例

const sales = [
    { product: 'A', amount: 100 },
    { product: 'B', amount: 200 },
    { product: 'A', amount: 150 }
];

const total = sumifObj(sales, 'product', 'A', 'amount');
console.log(total); // 输出 250 (100 + 150)

这些实现方法覆盖了基本和高级的 SUMIF 功能需求,可以根据实际场景进行调整或扩展。

标签: sumifjs
分享给朋友:

相关文章

jquery.js

jquery.js

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互等操作。以下是关于 jQuery.js 的核心信息和使用方法: 获取 jQ…

节流js实现

节流js实现

节流(Throttle)的实现原理 节流是一种限制函数执行频率的技术,确保函数在一定时间间隔内最多执行一次。适用于高频触发事件(如滚动、输入、窗口调整等)的场景。 基础实现方式 使用时间戳判断是否执…

js节流实现

js节流实现

节流的概念 节流(Throttle)是一种限制函数执行频率的技术,确保函数在一定时间间隔内只执行一次。常用于滚动事件、窗口调整等高频触发的场景。 基础实现方法 使用时间戳判断是否执行函数: fun…

js实现正交

js实现正交

正交的概念 正交在数学和计算机科学中通常指两个向量垂直或线性无关。在编程中,正交性常被用于设计模块化、低耦合的系统。 向量正交判断 判断两个向量是否正交可以通过点积是否为0来实现: fun…

js实现吸色

js实现吸色

使用Canvas实现吸色功能 通过Canvas的getImageData方法获取像素颜色数据。创建一个Canvas元素,将目标图像绘制到Canvas上,通过鼠标事件获取坐标对应的颜色值。 const…

js实现文字滚动

js实现文字滚动

实现文字滚动的几种方法 使用CSS动画实现滚动 通过CSS的@keyframes和transform属性可以实现平滑的文字滚动效果。 <style> .scroll-text { w…