You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于指定总量填充对象数组amountFilled字段的实现需求

数组填充与总量扣除逻辑实现

需求说明

现有一个包含数量属性的对象数组,每个对象包含amountToFill(待填充值)和amountFilled(已填充值)字段。要求:

  • 给定初始总量amount=50,自动将amountFilled填充至amountToFill的数值,同时逐步扣除amount,直至amount为0
  • 若amountFilled初始值大于0,计算总量扣除时仅针对还需填充的差值(即amountToFill - amountFilled)

初始数组

amountArray = [
  {
    option:'text',
    amountToFill: 5,
    amountFilled:0,
  },
  {
    option:'text1',
    amountToFill: 3,
    amountFilled:1,
  },
  {
    option:'text2',
    amountToFill: 10,
    amountFilled:0,
  },
  {
    option:'text3',
    amountToFill: 4,
    amountFilled:1,
  },
  {
    option:'text4',
    amountToFill: 4,
    amountFilled:1,
  },
  {
    option:'text5',
    amountToFill: 15,
    amountFilled:0,
  },
  {
    option:'text6',
    amountToFill: 5,
    amountFilled:2,
  },
  {
    option:'text7',
    amountToFill:25,
    amountFilled:3,
  },
  {
    option:'text8',
    amountToFill: 15,
    amountFilled:0,
  },
  {
    option:'text9',
    amountToFill: 25,
    amountFilled:0,
  },
  {
    option:'text10',
    amountToFill: 5,
    amountFilled:0,
  }
];

期望结果

amountArray1 = [
  {
    option:'text',
    amountToFill: 5,
    amountFilled:5,
    filled: true
  },
  {
    option:'text1',
    amountToFill: 3,
    amountFilled:3,
    filled: true
  },
  {
    option:'text2',
    amountToFill: 10,
    amountFilled:10,
    filled: true
  },
  {
    option:'text3',
    amountToFill: 4,
    amountFilled:4,
    filled: true
  },
  {
    option:'text4',
    amountToFill: 4,
    amountFilled:4,
    filled: true
  },
  {
    option:'text5',
    amountToFill: 15,
    amountFilled:15,
    filled: true
  },
  {
    option:'text6',
    amountToFill: 5,
    amountFilled:5,
    filled: true
  },
  {
    option:'text7',
    amountToFill:25,
    amountFilled:12,  // 此时amount已耗尽为0
    filled: true  
  },
  {
    option:'text8',
    amountToFill: 15,
    amountFilled:0,
    filled: false 
  },
  {
    option:'text9',
    amountToFill: 25,
    amountFilled:0,
    filled: false 
  },
  {
    option:'text10',
    amountToFill: 5,
    amountFilled:0,
    filled: false 
  }
];

实现逻辑

  1. 维护剩余可分配的amount值,遍历数组每个对象
  2. 计算当前对象还需填充的数量:need = amountToFill - amountFilled
  3. 剩余amount >= need时:
    • 将amountFilled设为amountToFill
    • 剩余amount扣除need
    • 标记filled: true
  4. 剩余amount < need时:
    • 若剩余amount > 0:把剩余量全部分配给当前对象,amount置0,标记filled: true
    • 若剩余amount == 0:保持原amountFilled不变,标记filled: false

示例代码

function fillAmounts(originalArray: any[], initialAmount: number) {
  let remainingAmount = initialAmount;
  // 深拷贝原数组,避免修改原数据
  return originalArray.map(item => {
    const newItem = {...item};
    const need = newItem.amountToFill - newItem.amountFilled;
    
    if (remainingAmount <= 0) {
      newItem.filled = false;
      return newItem;
    }

    if (need <= remainingAmount) {
      newItem.amountFilled = newItem.amountToFill;
      remainingAmount -= need;
      newItem.filled = true;
    } else {
      newItem.amountFilled += remainingAmount;
      remainingAmount = 0;
      newItem.filled = true;
    }
    return newItem;
  });
}

// 使用示例
const amountArray = [/* 初始数组内容 */];
const result = fillAmounts(amountArray, 50);
console.log(result);

内容的提问来源于stack exchange,提问作者John w.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 18:10:26