3

I've been trying to do the following: I make an array containing an URL list, comming from my selected emails and doubles filtered out. I want to sort these URL's on provider in a multidimensional array. I've been searching and trying different approaches but can't get it to work...

I want to create a multidimensional array that looks like providers[z][y] where [z] contains the list of providers where the mails come from and [y] is the array containing the URL list from provider[z].

I have tried to declare the array providers in multiple ways: var providers = [ [], [], [] ]; (also added more brackets just to be sure there where enough for all URL's in the list), var providers = [];, var providers = {}; and some more variations.

The code i created to sort them looks like this:

    for (var y = 0; y < urlarray.length; y++) {
        var pos1;
        provider = urlarray[y].split(".")[1];
        if (providers.indexOf(provider) === -1) {
            providers.push(provider);
        }
        pos1 = providers.indexOf(provider);
        providers[pos1] = providers[pos1] || []; // should add an array???
        providers[pos1].push(urlarray[y]);
    }

The code seems to stop after the push because array providers[0][0] is undefined. Thunderbird doesn't seem to add a new y array with providers[pos1] = providers[pos1] || [];.

How can i make this code work so provider[z][y] contains URL's from provider[z]?

And as a second question, if i want get the biggest [y] array, is the only way to acomplish this to use a for statement to count all objects and compare per provider which is the biggest number or is there a shorter way to get this done? Example how i would do this:

    var biggestarray = 0;
    for (var a = 0; a < providers.length; a++) {
        var counter = 0;
        for (var b = 0; b < 99; b++) { // kept it much bigger then needed
            if (provider[a][b] != undefined) {
                counter = b + 1;
            } else break;
        }
        if (biggestarray < counter) {
            biggestarray = counter;
        }
    }

Is there another way to make b run until the length of provider[a][b] instead of above solution that runs 99 times or until the break?

Hope you can get me on the right path again, getting crazy of this not working how i want ;)

Thanks in advance.

Greets ProphecyNL.

ProphecyNL
  • 33
  • 5
  • do you have some examples of your data? – Nina Scholz Apr 21 '16 at 09:45
  • 1
    yes i stream all selected emails and save the anchors i search for to the array `urlarray`, which looks like `var urlarray = ['url1', 'url2', etc]`. I want to access them like `providers[provider][url]` if possible. – ProphecyNL Apr 21 '16 at 10:16
  • thanks, but you can not split `url1`. does it look like `example.com`? – Nina Scholz Apr 21 '16 at 10:27
  • the full url is like `https://www.example.com/sub/somepage.php`. i only split it to get the provider and use it as ID. I get multiple emails from providerA, providerB and so on, and want all links from providerA linked to that array. – ProphecyNL Apr 21 '16 at 10:39

2 Answers2

1

This is proposal with a single loop and an object for the grouped result.

var urlarray = ['http://www.exa.com', 'http://www.example.com', 'http://www.test.org', 'http://www.test.info'],
    providers = {},
    biggest = [];

urlarray.forEach(function (a, i) {
    var provider = a.split(".")[1];
    providers[provider] = providers[provider] || [];
    providers[provider].push(a);
    if (!i || providers[biggest[0]].length < providers[provider].length) {
        biggest = [provider];
        return;
    }
    if (providers[biggest[0]].length === providers[provider].length) {
        ~biggest.indexOf(provider) || biggest.push(provider);
    }
});

document.write(biggest);
document.write('<pre>' + JSON.stringify(providers, 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Thanx, but it seems mozilla thunderbird doesn't want to execute the code correctly... I was using TB 38.xxx and have upgraded to 45 but still nothing. – ProphecyNL Apr 21 '16 at 21:01
  • it works in ie11, edge and chrome. ff, don't know. btw, it is es 5 – Nina Scholz Apr 21 '16 at 21:05
  • 1
    Ok it is working... stupid mistake of me because i was reading `providers[0][0]` instead of `providers['providerA'][0]`. That was the mistake i was making the whole time, i have been out of programming for too long ;) Thanx for your help!!! :) – ProphecyNL Apr 21 '16 at 21:24
0

I want to sort these URL's on provider in a multidimensional array

To sort all the provider's URLs try,

providers.forEach(function(urlArr){
   urlArr.sort();
});

To get the provider's index with biggest array

var biggestArrayIndex = -1;
var biggestArrayLength = 0;
providers.forEach(function(value, index){
   if (value.length > biggestArrayLength )
   {
      biggestArrayLength = value.length;
      biggestArrayIndex = index;
   }
})

Now the provider's index with biggest array is biggestArrayIndex. You can get that array by doing - providers[biggestArrayIndex]

gurvinder372
  • 66,980
  • 10
  • 72
  • 94