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.