1

So I have an array of objects that looks like:

dataArray = [
    {Revenue: 5, Date: "2018-06-05T00:00:00", DateString: "6/5/2018"},
    {Revenue: 10, Date: "2018-06-05T00:00:00", DateString: "6/5/2018"},
    {Revenue: 5, Date: "2018-06-05T00:00:00", DateString: "6/5/2018"},
    {Revenue: 1, Date: "2018-06-13T00:00:00", DateString: "6/13/2018"},
    {Revenue: 4, Date: "2018-06-13T00:00:00", DateString: "6/13/2018"}
]

And what I need is to reduce that down to another array of objects that looks like:

reducedDataArray = [
    {Revenue: 20, Date: "2018-06-05T00:00:00", DateString: "6/5/2018"},
    {Revenue: 5, Date: "2018-06-13T00:00:00", DateString: "6/13/2018"}
]

Basically if the date is the same then the revenue needs to be summed up so that there aren't multiple instances of the same date. Unfortunately I am limited to our version of Typescript so I don't have all the cool ES6 goodies to help with this stuff. I've tried using things like reduce() but I think a combination of not knowing what the heck I'm doing and this being kind of weird has left me in a fog.

Any suggestions or insight would be greatly appreciated!

2
  • 2
    Add the code you've tried to your question. Commented Jun 19, 2018 at 19:59
  • 1
    you don't need anything fancy to loop through and add totals by key (date) to a blank object, which you can then turn back into an array, deriving datestring from date; ES3 is plenty... Commented Jun 19, 2018 at 20:02

2 Answers 2

3

You can use reduce to create an object with date keys and then convert the object back to an array.

let dataArray = [
    {Revenue: 5, Date: "2018-06-05T00:00:00", DateString: "6/5/2018"},
    {Revenue: 10, Date: "2018-06-05T00:00:00", DateString: "6/5/2018"},
    {Revenue: 5, Date: "2018-06-05T00:00:00", DateString: "6/5/2018"},
    {Revenue: 1, Date: "2018-06-13T00:00:00", DateString: "6/13/2018"},
    {Revenue: 4, Date: "2018-06-13T00:00:00", DateString: "6/13/2018"}
]
// We need to specify the type parameter explicitly, as it will not be inffered from initial value 
let valuesObj = dataArray.reduce<{ [date: string]: typeof dataArray[number] }>((p, v) => {
    const existing = p[v.Date];
    if (existing) {
        existing.Revenue += v.Revenue
    } else {
        p[v.Date] = Object.assign({}, v); //Create a copy so as to not change the original when we sum 
    }
    return p;
}, {});


// You can use Object.values(valuesObj) if available 
let values = Object.keys(valuesObj).map(k => valuesaObj[k]);

Playground link

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

2 Comments

@prail and from my phone too :-P
Thank you very much for taking the time to write this out. I really appreciate it.
1

Played with some loops and it works

dataArray = [
    {Revenue: 5, Date: "2018-06-05T00:00:00", DateString: "6/5/2018"},
    {Revenue: 10, Date: "2018-06-05T00:00:00", DateString: "6/5/2018"},
    {Revenue: 5, Date: "2018-06-05T00:00:00", DateString: "6/5/2018"},
    {Revenue: 1, Date: "2018-06-13T00:00:00", DateString: "6/13/2018"},
    {Revenue: 4, Date: "2018-06-13T00:00:00", DateString: "6/13/2018"}
];

const getUnique=array=>{
  return array.filter((value, index, self)=> { 
      return self.indexOf(value) === index;
  });
}

let date=[];
let output=[];

for(const v of dataArray) date.push(v.Date);
for(const v of getUnique(date)){

  let revenue=0,date=v,dateString;
  for(const x of dataArray){
    if(x.Date==v) revenue+=x.Revenue;
    dateString=x.DateString;
  }

  output.push({
    Revenue:revenue,
    Date:v,
    DateString:dateString
  });

}

console.log(output);

jsfiddle link

1 Comment

Thank you so much! I'm marking this as the answer because it follows a similar pattern to other code I'm working with.

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.