当前位置:首页 > 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
分享给朋友:

相关文章

js实现轮播

js实现轮播

实现轮播图的基本思路 轮播图的核心逻辑是通过定时切换展示的图片或内容,通常结合左右滑动按钮和指示器(小圆点)增强交互性。以下是基于原生JavaScript的实现方法。 HTML结构 构建轮播图的H…

jquery.js

jquery.js

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

js 实现vue

js 实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,可以通过数据绑定、响应式系统和虚拟 DOM 来实现。以下是实现 Vue.js 核心功能的简化版本。 数据响应式系…

js实现论坛

js实现论坛

实现论坛的基本功能 使用JavaScript实现一个论坛需要结合前端和后端技术。前端可以使用React、Vue或Angular等框架,后端可以选择Node.js配合Express或Koa框架。数据库可…

js类实现

js类实现

使用类实现 JavaScript 功能 在 JavaScript 中,类(Class)是一种语法糖,基于原型继承机制。通过 class 关键字可以更直观地定义对象模板。 基本类定义 class…

js实现选题

js实现选题

实现选题功能的JavaScript方法 基础实现方案 使用数组存储选项,通过随机索引选取: const options = ['选项A', '选项B', '选项C', '选项D']; const…