0

Hi I'm stuck with this problem. Hopefully somebody can give me some advice.

I've got a JSON with the last day changes in a application that I work on. I want to get 3 datasets from it. And want to keep de right order in date like the example below:

"log": [{
        "objectname": "Lorem Ipsum",
        "type": "CREATE",
        "timenormal": "Sun Feb 09 2020 22:02:26 GMT+0100",
        "closesthours": 22
    }, {
        "objectname": "This is a Dummy",
        "type": "CREATE",
        "timenormal": "Sun Feb 09 2020 23:27:46 GMT+0100",
        "closesthours": 23
    },{
        "objectname": "Deleted test",
        "type": "DELETE",
        "timenormal": "Mon Feb 10 2020 00:30:14 GMT+0100",
        "closesthours": 1
    },{
        "objectname": "Qwerty",
        "type": "CREATE",
        "timenormal": "Mon Feb 10 2020 00:45:04 GMT+0100",
        "closesthours": 1
    },{
        "objectname": "Deleted test",
        "type": "DELETE",
        "timenormal": "Mon Feb 10 2020 04:45:14 GMT+0100",
        "closesthours": 5
    }, {
        "objectname": "Hello World",
        "type": "CREATE",
        "timenormal": "Mon Feb 10 2020 10:51:22 GMT+0100",
        "closesthours": 11
    }, {
        "objectname": "Another one",
        "type": "CREATE",
        "timenormal": "Mon Feb 10 2020 10:41:22 GMT+0100",
        "closesthours": 11
    }, {
        "objectname": "ABC",
        "type": "CREATE",
        "timenormal": "Mon Feb 10 2020 21:16:57 GMT+0100",
        "closesthours": 21
    }, {
        "objectname": "test dummy",
        "type": "DELETE",
        "timenormal": "Mon Feb 10 2020 21:17:00 GMT+0100",
        "closesthours": 21
    }
]

I know that following is possible to use map for the 'closesthours':

jsondata['log'].map(function(value, index) {return value['closesthours']});
[22, 23, 1, 1, 5, 11, 11, 21, 21]

From there on, I'm stuck. The 3 datasets I want is:

1.) A map with the closesthours combined:

{"hours":"22,23,1,5,11,21"}

2 & 3) Then I realy want to get a count for occurences by closesthours in the dataset for the two types (DELETE and CREATE) For example, I want to get the following back(with the 0 if that type doesn't have te closesthours):

{"type":"CREATE","groupcount":"1,1,1,0,2,1"}
{"type":"DELETE","groupcount":"0,0,1,1,0,1"}

How can I do this in javascript?

Juraj
  • 192
  • 2
  • 9
  • For getting the map of all closest hours: https://stackoverflow.com/q/19590865/215552, but you should invest some time in learning the [`reduce` function](https://stackoverflow.com/q/5732043/215552) – Heretic Monkey Feb 10 '20 at 21:09

1 Answers1

1

You could first build one object with unique hours array and every type and then use another loop to fill groupcount array for each type using previously created object.

const data = [{"objectname":"Lorem Ipsum","type":"CREATE","timenormal":"Sun Feb 09 2020 22:02:26 GMT+0100","closesthours":22},{"objectname":"This is a Dummy","type":"CREATE","timenormal":"Sun Feb 09 2020 23:27:46 GMT+0100","closesthours":23},{"objectname":"Deleted test","type":"DELETE","timenormal":"Mon Feb 10 2020 00:30:14 GMT+0100","closesthours":1},{"objectname":"Qwerty","type":"CREATE","timenormal":"Mon Feb 10 2020 00:45:04 GMT+0100","closesthours":1},{"objectname":"Deleted test","type":"DELETE","timenormal":"Mon Feb 10 2020 04:45:14 GMT+0100","closesthours":5},{"objectname":"Hello World","type":"CREATE","timenormal":"Mon Feb 10 2020 10:51:22 GMT+0100","closesthours":11},{"objectname":"Another one","type":"CREATE","timenormal":"Mon Feb 10 2020 10:41:22 GMT+0100","closesthours":11},{"objectname":"ABC","type":"CREATE","timenormal":"Mon Feb 10 2020 21:16:57 GMT+0100","closesthours":21},{"objectname":"test dummy","type":"DELETE","timenormal":"Mon Feb 10 2020 21:17:00 GMT+0100","closesthours":21}]

const {hours, types} = data.reduce((r, {type, closesthours}, i) => {
  r.hours = [...new Set([...r.hours, closesthours] || [])]
  r.types[type] = {type, groupcount: []}
  return r;
}, {hours: [], types: {}});

data.forEach(({type, closesthours}) => {
  const index = hours.indexOf(closesthours);
  for(let t in types) {
    if(types[t].groupcount[index] == undefined) {
      types[t].groupcount[index] = 0
    }
    
    if(t == type) {
      types[t].groupcount[index] += 1
    } 
  } 
});

console.log(hours)
console.log(Object.values(types))
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176