gpt4 book ai didi

javascript - 如何根据多笔费用分摊金额拆分费用给不同份额的多个用户?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:39:49 24 4
gpt4 key购买 nike

我想根据以下条件分摊费用:

1. Expense By user.
2. Divided by users.
3. Share of each user.

我想要输出欠其他用户钱的用户列表。我尝试过以不同的方式进行计算,但没有任何帮助。

如果有人能提供帮助,将不胜感激......

输入Json:

var expenseObj = [
{
expenseBy:{
user:"A",
amount:1000
},
dividedBy: [
{
user:"A",
amount:100
},{
user:"B",
amount:500
},{
user:"C",
amount:400
}
]
},
{
expenseBy:{
user:"B",
amount:2000
},
dividedBy: [
{
user:"A",
amount:1000
},{
user:"B",
amount:400
},{
user:"C",
amount:600
}
]
}
]

最佳答案

  for (let i = 0; i<expenseObj.length; i++){
var share = expenseObj[i].expenseBy.amount/expenseObj[i].dividedBy.length;
var addedBy = expenseObj[i].expenseBy.user;
var addedAmount = expenseObj[i].expenseBy.amount;
for(j=0; j<expenseObj[i].dividedBy.length;j++){
var amount = share-expenseObj[i].dividedBy[j].amount
if( amount >= 0){
console.log(expenseObj[i].dividedBy[j].user+" owes "+amount+" in expense added by "+addedBy+" of amount "+addedAmount);
}
}
}

我希望它能让你对你想做什么有一些基本的了解。

关于javascript - 如何根据多笔费用分摊金额拆分费用给不同份额的多个用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52741941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com