Reputation: 973
I have an object:
const transaction = [{
created: 1200,
amount: 200
},{
created: 1200,
amount: 400
},{
created: 1400,
amount: 400
},{
created: 1400,
amount: 300
},{
created: 1500,
amount: 100
}]
Everytime created
has the same value I want to sum the amount
's of the objects. So for example created: 1200
would have an amount of: 600
in total, because multiple created
's where at 1200 so the amount got summed up.
Upvotes: 0
Views: 1026
Reputation: 4267
const transaction = [{
created: 1200,
amount: 200
},{
created: 1200,
amount: 400
},{
created: 1400,
amount: 400
},{
created: 1400,
amount: 300
},{
created: 1500,
amount: 100
}];
function getComputedTransaction(transHistory) {
let tempHistory = [];
transHistory.forEach((item)=>{
let noMatch = true; // temp created match conditioner
if(tempHistory.length > 0) {
tempHistory.forEach((tempItem, i)=>{
if(item.created === tempItem.created) {
tempHistory[i].amount += item.amount;
noMatch = !noMatch; // make noMatch = false
}
});
}
return (noMatch) ? tempHistory.push(item) : null;
});
return tempHistory;
}
console.log(getComputedTransaction(transaction));
Upvotes: 0
Reputation: 2829
Here is a simple solution, note that I coded it fast so it can be optimized later
const transaction = [{
created: 1200,
amount: 200
},{
created: 1200,
amount: 400
},{
created: 1400,
amount: 400
},{
created: 1400,
amount: 300
},{
created: 1500,
amount: 100
}];
// accepts an array of objects
function sumObj(objArr) {
// an object to store the `created` and `amount` as key=>value
var newObj = {};
// loop over the objects in the array
objArr.forEach(function(obj) {
// if the key is present in `newObj` then we need to add the amount to it's value
if(obj.created in newObj) {
newObj[obj.created] += obj.amount;
}else {
// else just add the key and the amount as value
newObj[obj.created] = obj.amount;
}
});
// create an array to store the final objects
var arr = [];
// loop over the properties in `newObj`
for(var prop in newObj) {
// push an object each time
arr.push({created: Number(prop), amount: newObj[prop]});
}
// return the final result
return arr;
}
// log it to see the output
console.log(sumObj(transaction));
Note: I have noticed that each object in the array is sorted according to .created
so I came up with another solution, note that if the objects are not always sorted then use the first solution instead
const transaction = [{
created: 1200,
amount: 200
},{
created: 1200,
amount: 400
},{
created: 1400,
amount: 400
},{
created: 1400,
amount: 300
},{
created: 1500,
amount: 100
}];
// accepts an array of objects
function sumObj(objArr) {
// create an array to store the objects
var newArr = [];
// loop over the objects in the array
objArr.forEach(function(obj, ind, arr) {
// so since we check for the current object against the next one
// we need to check when there is no next one to avoid errors
// the idea is to check if the current object has the same .created
// of the next object then add the amount to the next object
// else check if this is the last object then just push it to the array
// or if the current object has a different .created value then
// just push it
if(ind === arr.length - 1 || obj.created !== arr[ind + 1].created) {
newArr.push(obj);
}else {
arr[ind + 1].amount += obj.amount;
}
});
// return the result
return newArr;
}
// print the result
console.log(sumObj(transaction));
Upvotes: 1