#javascript #node.js #arrays
#javascript #node.js #массивы
Вопрос:
Привет, у меня есть данные json, в которых у меня есть несколько записей, записанных с меткой времени. я хочу отфильтровать записи и создать новый подмассив, который сгруппирует все записи с часовым интервалом.
как будто у меня есть 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 }
]
Теперь я хочу вывести новый массив json с IP-адресами, сгруппированными по временному интервалу.
Что я сделал, так это этот код
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)
код работает нормально. но я не думаю, что это лучший подход, потому что при этом будет выполняться много итерационных циклов, и я хочу сделать его экономически эффективным. кто-нибудь может подсказать мне наилучший возможный способ или может написать код для меня?
вы можете использовать любой пакет npm, который вы хотите.
добавление скрипки для справки — https://jsfiddle.net/gku24x9m /
Комментарии:
1. Можете ли вы добавить вывод к своему результату.?
2. @JustRaman — добавлена скрипка в вопросе, пожалуйста, проверьте
Ответ №1:
Создайте карту с данными группы hours
, а затем преобразуйте ее в список A.
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)
Ответ №2:
Вы можете группировать по часовому интервалу за один проход, уменьшая (группируя) arr
в карту на основе часа каждой временной метки, а затем преобразуя значения карты в нужный массив.
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)