0

I have an object with keys as dates in the following format "yyyy-mm-dd" (ex: 2020-08-14) the value of each key is an array of objects with two attributes, name and value.

I need to find a way to get the sum of the keyvalue grouped by name accros N days.

Here is an example to better understand, the original object have 4 days of data, with one day having an empty array:

{
  "2020-10-15":[
    {"name":"France","value":20},
    {"name":"United States","value":20},
    {"name":"Italy","value":5},
   ],
 "2020-10-16":[
    {"name":"Germany","value":10},
    {"name":"United States","value":5},
    {"name":"France","value":10}
   ],
  "2020-10-17":[],
   "2020-10-18":[
    {"name":"UK","value":10},
    {"name":"Italy","value":10},
   ]

 }

For example if we wish to group this data by 4 days we will get the following:

[{"name": "France", "value": 30},
 {"name": "United States", "value": 25},
 {"name": "Italy", "value": 15},
 {"name": "Germany", "value": 10},
 {"name": "UK", "value": 10}]

This is the sum of all objects with same value for name across 4 days. I have absolutly no idea how to achieve this, I know can use map to iterate the object keys and do some processing through moment js but I don't know how to achieve this.

1
  • start the way you do know how to do it: don't try to use clever .map or .entries etc, just create an empty array, start a for loop, and have your code do things the more verbose, much at least understandable-to-yourself way. Commented Nov 17, 2020 at 19:24

1 Answer 1

2

You can do the following using reduce, forEach and Object.keys method,

const value = {
  "2020-10-15":[
    {"name":"France","value":20},
    {"name":"United States","value":20},
    {"name":"Italy","value":5},
   ],
 "2020-10-16":[
    {"name":"Germany","value":10},
    {"name":"United States","value":5},
    {"name":"France","value":10}
   ],
  "2020-10-17":[],
   "2020-10-18":[
    {"name":"UK","value":10},
    {"name":"Italy","value":10},
   ]

 }

let res = Object.keys(value).reduce((prev, curr) => {
   value[curr].forEach(item => {
      const idx = prev.findIndex(pItem => pItem.name === item.name);
      if(idx > -1) {
         const newObj = {...prev[idx], value: prev[idx].value + item.value};
         prev[idx] = newObj;
         return ;
      }
      prev.push(item);
   })
   return prev;
}, []);
console.log(res);
Sign up to request clarification or add additional context in comments.

2 Comments

This code works perfectly thank you but I couldn't really understand it, could you add some explanation to your answer please ?
I will add an explanation tomorrow. In the mean time you can read the links I have provided. It should be sufficient to get some idea whats happenning here.

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.