Reputation: 973
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
var arr= [
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 02:02:58", "amount": 7.00 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 02:12:32", "amount": 6.50 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 02:13:11", "amount": 7.25 },
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 02:13:54", "amount": 8.75 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 05:02:45", "amount": 11.00 },
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 06:32:42", "amount": 5.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 06:35:12", "amount": 2.00 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 06:45:01", "amount": 12.00 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 06:59:59", "amount": 11.75 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 07:01:53", "amount": 1.00 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 07:02:54", "amount": 4.50 },
{ "ip":"33.33.33.33", "timestamp":"3/11/2016 07:02:54", "amount": 15.75 },
{ "ip":"66.66.66.66", "timestamp":"3/11/2016 07:02:54", "amount": 14.25 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 07:03:15", "amount": 12.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 08:02:22", "amount": 3.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 09:41:50", "amount": 4.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 10:02:54", "amount": 5.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 11:05:35", "amount": 10.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 13:02:21", "amount": 6.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:02:40", "amount": 8.00 },
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 13:02:55", "amount": 8.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:33:34", "amount": 8.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:42:24", "amount": 8.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:47:44", "amount": 6.25 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 14:02:54", "amount": 4.25 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 14:03:04", "amount": 5.25 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 15:12:55", "amount": 6.25 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 16:02:36", "amount": 8.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 16:22:11", "amount": 8.50 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 17:18:19", "amount": 11.25 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 18:19:20", "amount": 9.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 23:59:59", "amount": 9.00 }
]
Now i want to output a new array of json with the ips grouped in time interval.
What i have done is this code
var arr2 = [];
arr.forEach(function(itm) {
var now = new Date(itm.timestamp),
put = arr2;
arr2.forEach(function(itm2) {
itm2.forEach(function(itm3) {
var d = new Date(itm3.timestamp);
if (d.getHours() == now.getHours()) {
put = itm2;
}
});
});
put.push(put == arr2 ? [itm] : itm);
});
console.log('arr1', arr2)
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/
Upvotes: 1
Views: 178
Reputation: 1161
Create a map with group data by hours
then convert into a a list.
var arr = [
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 02:02:58", "amount": 7.00 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 02:12:32", "amount": 6.50 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 02:13:11", "amount": 7.25 },
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 02:13:54", "amount": 8.75 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 05:02:45", "amount": 11.00 },
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 06:32:42", "amount": 5.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 06:35:12", "amount": 2.00 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 06:45:01", "amount": 12.00 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 06:59:59", "amount": 11.75 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 07:01:53", "amount": 1.00 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 07:02:54", "amount": 4.50 },
{ "ip":"33.33.33.33", "timestamp":"3/11/2016 07:02:54", "amount": 15.75 },
{ "ip":"66.66.66.66", "timestamp":"3/11/2016 07:02:54", "amount": 14.25 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 07:03:15", "amount": 12.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 08:02:22", "amount": 3.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 09:41:50", "amount": 4.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 10:02:54", "amount": 5.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 11:05:35", "amount": 10.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 13:02:21", "amount": 6.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:02:40", "amount": 8.00 },
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 13:02:55", "amount": 8.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:33:34", "amount": 8.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:42:24", "amount": 8.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:47:44", "amount": 6.25 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 14:02:54", "amount": 4.25 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 14:03:04", "amount": 5.25 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 15:12:55", "amount": 6.25 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 16:02:36", "amount": 8.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 16:22:11", "amount": 8.50 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 17:18:19", "amount": 11.25 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 18:19:20", "amount": 9.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 23:59:59", "amount": 9.00 }
]
var resultSet = {};
arr.forEach(function(item) {
var hour = new Date(item.timestamp).getHours();
if(resultSet[hour] !== undefined){
return resultSet[hour].push(item);
}
return resultSet[hour] = [item];
});
const resultList = Object.values(resultSet);
console.log(resultList)
Upvotes: 2
Reputation: 2452
You can group by hour interval in a single pass by reducing (grouping) arr
into a map based on each timestamp's hour, then converting the map's values into the desired array.
var arr= [
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 02:02:58", "amount": 7.00 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 02:12:32", "amount": 6.50 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 02:13:11", "amount": 7.25 },
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 02:13:54", "amount": 8.75 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 05:02:45", "amount": 11.00 },
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 06:32:42", "amount": 5.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 06:35:12", "amount": 2.00 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 06:45:01", "amount": 12.00 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 06:59:59", "amount": 11.75 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 07:01:53", "amount": 1.00 },
{ "ip":"11.11.11.11", "timestamp":"3/11/2016 07:02:54", "amount": 4.50 },
{ "ip":"33.33.33.33", "timestamp":"3/11/2016 07:02:54", "amount": 15.75 },
{ "ip":"66.66.66.66", "timestamp":"3/11/2016 07:02:54", "amount": 14.25 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 07:03:15", "amount": 12.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 08:02:22", "amount": 3.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 09:41:50", "amount": 4.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 10:02:54", "amount": 5.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 11:05:35", "amount": 10.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 13:02:21", "amount": 6.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:02:40", "amount": 8.00 },
{ "ip":"44.44.44.44", "timestamp":"3/11/2016 13:02:55", "amount": 8.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:33:34", "amount": 8.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:42:24", "amount": 8.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 13:47:44", "amount": 6.25 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 14:02:54", "amount": 4.25 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 14:03:04", "amount": 5.25 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 15:12:55", "amount": 6.25 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 16:02:36", "amount": 8.00 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 16:22:11", "amount": 8.50 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 17:18:19", "amount": 11.25 },
{ "ip":"55.55.55.55", "timestamp":"3/11/2016 18:19:20", "amount": 9.00 },
{ "ip":"22.22.22.22", "timestamp":"3/11/2016 23:59:59", "amount": 9.00 }
]
var arr2= [...arr.reduce((hourLogsMap, log) => {
const hour = log.timestamp.split(' ')[1].split(':')[0]
if (hourLogsMap.has(hour)) {
hourLogsMap.get(hour).push(log)
} else {
hourLogsMap.set(hour, [log])
}
return hourLogsMap
}, new Map()).values()]
console.log('arr1', arr2)
Upvotes: 1