First time tackling a question, so I will strive to articulate the problem clearly.
I have two arrays containing different values and my goal is to map the values from the first array (which are inputs) to the second array (which are outputs).
If the value of the first object in the first array is smaller than the amount in the second array, it's as if the first object is transferring all its value to the second object. This process continues until the value from the second object is completely fulfilled.
This mapping should take place between objects, where even if the value in the first object is larger than the second object, a partial amount from the first object’s value is sent to fulfill the entirety of the second object, and then partially to the next one... Here are the arrays and an example of how it should end.
To achieve this, I am utilizing BigNumber.js and the formatUnit function for tidy numbers.
const firstArray = [
{ value: 0.001, sender: "one"},
{ value: 0.01, sender: "two"},
{ value: 0.1, sender: "three"},
{ value: 3.0, sender: "four"},
{ value: 0.002, sender: "five"},
{ value: 0.0003, sender: "six"},
{ value: 5.0, sender: "seven"}
]
const secondArray = [
{ value: 0.5, recipient: "a"},
{ value: 3.5, recipient: "b"},
{ value: 4.2133, recipient: "c"}
]
The desired output would look like this:
const thirdArray = [
{sender : one, receiver : a, amount : 0.001},
{sender : two, receiver : a, amount : 0.01},
{sender : three, receiver : a, amount : 0.1},
{sender : four, receiver : a, amount : 0.389},
{sender : four, receiver : b, amount : 2.611},
{sender : five, receiver : b, amount : 0.002},
{sender : six, receiver : b, amount : 0.0003},
{sender : seven, receiver : b, amount : 0.8867},
{sender : seven, receiver : c, amount : 4.2133}
]
Here is what I have come up with:
let i = 0;
let j = 0;
let thirdArray = [];
while (i < firstArray.length) {
let input = new BigNumber(firstArray[i].value);
while (j < secondArray.length) {
input = input.minus(new BigNumber(secondArray[j].value));
// Format units for amount
const formattedAmount = formatUnits(secondArray[j].value, -8);
// Initialize responseObject
const responseObj = {
sender: firstArray[i].sender,
receiver: secondArray[j].recipient,
amount: formattedAmount,
};
if (input.isLessThan(0)) {
let output = new BigNumber(secondArray[j].value);
output = output.minus(input.times(-1));
thirdArray.push({
...responseObj,
amount: formatUnits(output.toNumber(), -8),
});
output = input.times(-1);
break;
}
thirdArray.push(responseObj);
j += 1;
if (input.isEqualTo(0)) break;
}
i += 1;
}
console.log(thirdArray)
(current output)
[
{ sender: 'one', receiver: 'a', amount: '0.001' },
{ sender: 'two', receiver: 'a', amount: '0.01' },
{ sender: 'three', receiver: 'a', amount: '0.1' },
{ sender: 'four', receiver: 'a', amount: '0.5' },
{ sender: 'four', receiver: 'b', amount: '2.5' },
{ sender: 'five', receiver: 'b', amount: '0.002' },
{ sender: 'six', receiver: 'b', amount: '0.0003' },
{ sender: 'seven', receiver: 'b', amount: '3.5' },
{ sender: 'seven', receiver: 'c', amount: '1.5' }
]
Desired output:
[
{ sender : one, receiver : a, amount : 0.001 },
{ sender : two, receiver : a, amount : 0.01 },
{ sender : three, receiver : a, amount : 0.1 },
{ sender : four, receiver : a, amount : 0.389 },
{ sender : four, receiver : b, amount : 2.611 },
{ sender : five, receiver : b, amount : 0.002 },
{ sender : six, receiver : b, amount : 0.0003 },
{ sender : seven, receiver : b, amount : 0.8867 },
{ sender : seven, receiver : c, amount : 4.2133 }
]
Any assistance on this matter would be highly appreciated!