0

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.

5
  • 5
    What have you tried to solve this problem? Commented Aug 24, 2020 at 19:07
  • This may be helpful: stackoverflow.com/questions/14446511/… Commented Aug 24, 2020 at 19:08
  • @RobinZigmond well I have tried using Array.prototype.reduce to sum up the values. But I can't figure out the logic to only use it on the appriopriate object. Commented Aug 24, 2020 at 19:09
  • 1
    @Ezrab_ reduce sounds like the right idea - can you share your attempt? Commented Aug 24, 2020 at 19:19
  • 1
    Use an if statement in the callback you use in reduce, so you check for the value of created before summing. Commented Aug 24, 2020 at 19:37

2 Answers 2

1

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));

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

Comments

0

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));

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.