简体   繁体   中英

JS How to compare and count the same elements in each first multidimensional array?

I have this JS code:

$.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];
    });

By parts[0] I get each hostname how to compare them between each other and show the most repeatable? As I understand it's first elements of the multidimensional array.

Here is the example of log file:

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"

Just use another object to collect statistics.

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM