我想根据以下条件分配费用
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
}
]
}
]
答案 0 :(得分:0)
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);
}
}
}
我希望它会给您一些有关您想做什么的基本想法。