[英]JS. Loop through multidimensional array to count occurrences of elements in each column
[英]JS How to compare and count the same elements in each first multidimensional array?
我有這個JS代碼:
$.get('file.log', function(data) { var lines = data.split("\n"); $.each(lines, function(n, elem) { var url = elem.match(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig); var parts = url.toString().split(',h'); var file=parts[0]; var hostname='h' + parts[1]; });
通過部分[0],我得到每個主機名,如何將它們相互比較並顯示出最可重復的? 據我了解,這是多維數組的第一個元素。
這是日志文件的示例:
85.164.152.30 - - [23/May/2012:14:01:05 +0200] "GET http://www.vgtv.no/video/img/94949_160px.jpg HTTP/1.1" 200 3889 "http://www.vgtv.no/" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0"
只需使用另一個對象即可收集統計信息。
var hostnameFrequencies = {};
$.get('file.log', function (data) {
var lines = data.split("\n");
$.each(lines, function (n, elem) {
var url = elem.match(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig);
var parts = url.toString().split(',h');
var file = parts[0];
var hostname = 'h' + parts[1];
hostnameFrequencies[hostname] = hostnameFrequencies[hostname] ? hostnameFrequencies[hostname] + 1 : 1;
});
console.log(hostnameFrequencies);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.