0

I need to subtract total amount - paid amount which is available.scheduleBudget - paidAmount.paid

const available = [
  { scheduleBudget: 21600, schudule_number: "11020" },
  { scheduleBudget: 1598000, schudule_number: "11010" },
];

const paidAmount = [
  { paid: 88488, schudule_number: "11010" },
  { paid: 7140, schudule_number: "11020" },
];

Expected output :

[
  { balance: 1509512, schudule_number: "11010" },
  { balance: 14460, schudule_number: "11020" },
];

code to get output

Object.values(available).forEach((key) => {
  console.log(key);
});
1
  • Can either (or both) of available or paidAmount have items that share a schudule_number within their respective lists? i.e. Could two of the items in available have schudule_number 11020? Could two of the items in paidAmount have schudule_number 11020? Commented Apr 16, 2021 at 8:52

6 Answers 6

1

One way to do this is to generate a new object from paidAmount where schudule_number is the key and paid the value. Then you can simply map available to subtract the appropriate paid amount from scheduleBudget:

const available = [
  { scheduleBudget: 21600, schudule_number: "11020" },
  { scheduleBudget: 1598000, schudule_number: "11010" },
];

const paidAmount = [
  { paid: 88488, schudule_number: "11010" },
  { paid: 7140, schudule_number: "11020" },
];
 
const pA = {};
paidAmount.forEach(p => pA[p.schudule_number] = p.paid);

const result = available.map(({scheduleBudget, schudule_number}) => 
  ({ scheduleBudget: scheduleBudget-pA[schudule_number], schudule_number})
);

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

1

You could take an object for the balances groupd by schedule_number and get the result from the object.

This approach allows more than one payment for a schedule_number.

const
    available = [{ scheduleBudget: 21600, schedule_number: "11020" }, { scheduleBudget: 1598000, schedule_number: "11010" }],
    paidAmount = [{ paid: 88488, schedule_number: "11010" }, { paid: 7140, schedule_number: "11020" }],
    balances = Object.fromEntries(available.map(({ scheduleBudget, schedule_number }) =>
        [schedule_number, scheduleBudget]
    ));

paidAmount.forEach(({ paid, schedule_number }) => balanceses[schedule_number] -= paid);

const
    result = Object
    .entries(balances)
    .map(([schedule_number, balance]) => ({ balance, schedule_number }));

console.log(result);

Comments

0

So you do this to get the answer:

const available = [
  { scheduleBudget: 21600, schudule_number: '11020' },
  { scheduleBudget: 1598000, schudule_number: '11010' },
]

const paidAmount = [
  { paid: 88488, schudule_number: '11010' },
  { paid: 7140, schudule_number: '11020' },
]

let finalAvailable = []
available.forEach((value) => {
  paidAmount.forEach((item) => {
    if (value.schudule_number === item.schudule_number) {
      scheduleBudget = value.scheduleBudget - item.paid
      finalAvailable.push({ scheduleBudget, schudule_number: item.schudule_number })
    }
  })
  return finalAvailable
})

console.log('finalAvailable:', finalAvailable)

const available = [
  { scheduleBudget: 21600, schudule_number: '11020' },
  { scheduleBudget: 1598000, schudule_number: '11010' },
]

const paidAmount = [
  { paid: 88488, schudule_number: '11010' },
  { paid: 7140, schudule_number: '11020' },
]

let finalAvailable = []
available.forEach((value) => {
  paidAmount.forEach((item) => {
    if (value.schudule_number === item.schudule_number) {
      scheduleBudget = value.scheduleBudget - item.paid
      finalAvailable.push({ scheduleBudget, schudule_number: item.schudule_number })
    }
  })
  return finalAvailable
})

console.log('finalAvailable:', finalAvailable)

Comments

0

If you want use forEach you need to create a third object and forEach both object and substract the value when ticket fit together

const available = [{scheduleBudget: 21600, schudule_number: '11020'},
    {scheduleBudget: 1598000, schudule_number: '11010'}];

const paidAmount = [{paid: 88488, schudule_number: '11010'},
    {paid: 7140, schudule_number: '11020'}];
let newOutput = [];
Object.values(available).forEach(keyavailable => {
    Object.values(paidAmount).forEach(keypaidAmount => {
        if (keyavailable.schudule_number === keypaidAmount.schudule_number) {
            let sub = keyavailable.scheduleBudget - keypaidAmount.paid;
            let data = {'balance': sub, 'schudule_number': keypaidAmount.schudule_number};
            newOutput.push(data);
        }
    })
})
console.log(newOutput);

Comments

0

Assuming the order in available and paidAmount arrays not must match the schulude_number:

const available =  [ { scheduleBudget: 21600, schudule_number: '11020' },    { scheduleBudget: 1598000, schudule_number: '11010' } ];
const paidAmount =  [ { paid: 88488, schudule_number: '11010' },    { paid: 7140, schudule_number: '11020' } ];


function pay(budgets, paid) {
  const budgetObj = budgets.reduce((a,b) => {
    a[b.schudule_number] = {balance:b.scheduleBudget, schudule_number:b.schudule_number};
    return a;
  },{});
  paid.forEach(p => budgetObj[p.schudule_number].balance -= p.paid);
  return Object.values(budgetObj);
}
console.log(pay(available, paidAmount));

Comments

0

If both of available and paidAmount have items that share a schudule_number within their respective lists then perhaps something like this would work:

const available = [{ scheduleBudget: 21600, schudule_number: "11020" }, { scheduleBudget: 1598000, schudule_number: "11010" }];
const paidAmount = [{ paid: 88488, schudule_number: "11010" }, { paid: 7140, schudule_number: "11020" }];

const balances = [...available, ...paidAmount].reduce(
  (acc, item) => Object.assign(
    acc,
    {
      [item.schudule_number]: (acc[item.schudule_number] || 0) +
        (item.scheduleBudget || 0) - (item.paid || 0)
    }
  ),
  {}
);

const result = Object.entries(balances).map(
  ([schudule_number, balance]) => ({ schudule_number, balance })
);

console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.