typescript 满足对象数组中每行的数量

ql3eal8s  于 2022-11-26  发布在  TypeScript
关注(0)|答案(1)|浏览(155)

我有一个带有数量的数组对象。在这个数组中,我有一个键告诉我要填充的值(amountToFill),还有一个键告诉我已经填充的值(amountFilled)。这个想法是为了指示一个数量(amount:数量= 50;)的值,并自动使用与amountToFill相同的值填充amountFilled,但始终减少(amount:number = 50)的值是数量:数字= 50等于0。
如果amountFilled〉0,则在计算总金额的减法时必须小心。
DEMO

初步结果

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 
  }];
euoag5mw

euoag5mw1#

const 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, }];
  
 let total = 50;
 const res = amountArray.map(it => {
 const subtract = it.amountToFill - it.amountFilled;
 if (total - subtract > 0) {
    total -= subtract;
  return {...it, amountFilled: it.amountToFill, filled: !!total}
 }
 else {
   const newObj = {...it, amountFilled: it.amountFilled + total, filled: !!total}
   total = 0;
   return newObj;
 }
 });
  
console.log(res);

相关问题