Hi I have a json data where i have multiple entries recorded with the timestamp. i want to filter the records and make a new sub array that will group all the entries with an hour interval.
like i have the json
JavaScript
x
35
35
1
var arr= [
2
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 02:02:58", "amount": 7.00 },
3
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 02:12:32", "amount": 6.50 },
4
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 02:13:11", "amount": 7.25 },
5
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 02:13:54", "amount": 8.75 },
6
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 05:02:45", "amount": 11.00 },
7
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 06:32:42", "amount": 5.00 },
8
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 06:35:12", "amount": 2.00 },
9
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 06:45:01", "amount": 12.00 },
10
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 06:59:59", "amount": 11.75 },
11
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 07:01:53", "amount": 1.00 },
12
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 07:02:54", "amount": 4.50 },
13
{ "ip":"33.33.33.33", "timestamp":"3/11/2016 07:02:54", "amount": 15.75 },
14
{ "ip":"66.66.66.66", "timestamp":"3/11/2016 07:02:54", "amount": 14.25 },
15
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 07:03:15", "amount": 12.00 },
16
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 08:02:22", "amount": 3.00 },
17
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 09:41:50", "amount": 4.00 },
18
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 10:02:54", "amount": 5.00 },
19
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 11:05:35", "amount": 10.00 },
20
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 13:02:21", "amount": 6.00 },
21
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:02:40", "amount": 8.00 },
22
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 13:02:55", "amount": 8.00 },
23
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:33:34", "amount": 8.00 },
24
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:42:24", "amount": 8.00 },
25
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:47:44", "amount": 6.25 },
26
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 14:02:54", "amount": 4.25 },
27
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 14:03:04", "amount": 5.25 },
28
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 15:12:55", "amount": 6.25 },
29
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 16:02:36", "amount": 8.00 },
30
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 16:22:11", "amount": 8.50 },
31
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 17:18:19", "amount": 11.25 },
32
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 18:19:20", "amount": 9.00 },
33
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 23:59:59", "amount": 9.00 }
34
]
35
Now i want to output a new array of json with the ips grouped in time interval.
What i have done is this code
JavaScript
1
20
20
1
var arr2 = [];
2
3
arr.forEach(function(itm) {
4
var now = new Date(itm.timestamp),
5
put = arr2;
6
7
arr2.forEach(function(itm2) {
8
itm2.forEach(function(itm3) {
9
var d = new Date(itm3.timestamp);
10
if (d.getHours() == now.getHours()) {
11
12
put = itm2;
13
}
14
});
15
});
16
put.push(put == arr2 ? [itm] : itm);
17
});
18
19
console.log('arr1', arr2)
20
the code is running fine . but i dont think this is the best approach because this will run many iteration loops and i want to make it cost efficient. can anyone tell me the best possible way or can code for me ?
you can use any npm package you want.
adding fiddle for reference – https://jsfiddle.net/gku24x9m/
Advertisement
Answer
Create a map with group data by hours
then convert into a a list.
JavaScript
1
53
53
1
var arr = [
2
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 02:02:58", "amount": 7.00 },
3
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 02:12:32", "amount": 6.50 },
4
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 02:13:11", "amount": 7.25 },
5
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 02:13:54", "amount": 8.75 },
6
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 05:02:45", "amount": 11.00 },
7
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 06:32:42", "amount": 5.00 },
8
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 06:35:12", "amount": 2.00 },
9
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 06:45:01", "amount": 12.00 },
10
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 06:59:59", "amount": 11.75 },
11
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 07:01:53", "amount": 1.00 },
12
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 07:02:54", "amount": 4.50 },
13
{ "ip":"33.33.33.33", "timestamp":"3/11/2016 07:02:54", "amount": 15.75 },
14
{ "ip":"66.66.66.66", "timestamp":"3/11/2016 07:02:54", "amount": 14.25 },
15
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 07:03:15", "amount": 12.00 },
16
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 08:02:22", "amount": 3.00 },
17
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 09:41:50", "amount": 4.00 },
18
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 10:02:54", "amount": 5.00 },
19
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 11:05:35", "amount": 10.00 },
20
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 13:02:21", "amount": 6.00 },
21
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:02:40", "amount": 8.00 },
22
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 13:02:55", "amount": 8.00 },
23
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:33:34", "amount": 8.00 },
24
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:42:24", "amount": 8.00 },
25
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:47:44", "amount": 6.25 },
26
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 14:02:54", "amount": 4.25 },
27
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 14:03:04", "amount": 5.25 },
28
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 15:12:55", "amount": 6.25 },
29
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 16:02:36", "amount": 8.00 },
30
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 16:22:11", "amount": 8.50 },
31
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 17:18:19", "amount": 11.25 },
32
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 18:19:20", "amount": 9.00 },
33
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 23:59:59", "amount": 9.00 }
34
]
35
36
37
38
var resultSet = {};
39
40
arr.forEach(function(item) {
41
var hour = new Date(item.timestamp).getHours();
42
if(resultSet[hour] !== undefined){
43
return resultSet[hour].push(item);
44
}
45
return resultSet[hour] = [item];
46
});
47
48
const resultList = Object.values(resultSet);
49
50
console.log(resultList)
51
52
53