0

i want to for loop my array result as array because i use nested loop. Here is my code

var isp = ["yahoo", "gmail"];
var yahoo = ["@yahoo.com", "@rocketmail.com", "@ymail.com"];
var gmail = ["@gmail.com"];

for(x=0;x<isp.length;x++){
    //Should alert 3 Because var yahoo contains 3 element
    //Should alert 1 because var gmail is contain 1 element
    alert(isp[x].length);
    for(y=0;y<isp[x].length;y++){
        //Should alert @yahoo.com, @rocketmail.com and so on
        alert(isp[x][y]);
    }
}

Here is my JSFiddle https://jsfiddle.net/4v272ghL/1/

Dark Cyber
  • 2,181
  • 7
  • 44
  • 68

4 Answers4

2

Try this:

https://jsfiddle.net/4v272ghL/2/

var isp = ["yahoo", "gmail"];
var providers = {
    'yahoo': ["@yahoo.com", "@rocketmail.com", "@ymail.com"],
    'gmail': ["@gmail.com"]
};

isp.forEach(function(v, i) {
    console.log(v);
    providers[v].forEach(function(domain, index) {
        console.log(domain);
    });
});

You're using a JS object to hold the arrays of domains instead. Using this, you can access each provider's data dynamically.

Scheda
  • 526
  • 5
  • 11
1

Maybe you don't need so many arrays? Lets use an object instead.

var isps = {
    yahoo: ["@yahoo.com", "@rocketmail.com", "@ymail.com"],
    gmail: ["@gmail.com"]
};

for(isp in isps) {
    var providers = isps[isp];
  
    document.write(isp + " has " + providers.length + " provider(s):<br/>");

    providers.forEach(function(domain) {
        document.write(domain + "<br/>");
    });
};

This works because instead of looping through an array and trying to access different variables with the same name, you can instead simply loop through the keys of an object (which are the same as that first array) and use it to access the values of that object (which are the same as in the other variables you had before).

Note that I've changed your alerts to things that will be more informative in running the code snippet. Of course, once you've got access to these values isp, providers and domain, you can do whatever you like with them - you don't need to document.write them.

There are a few benefits to this method. For instance, as we're only human, what if this happened:

var isp = ["yahoo"];
var yahooo = ["@yahoo.com"];

There's a dependency on the values in isp and the variable names being exactly the same. A simple error like above ("yahooo" instead of "yahoo") would prevent the code from working, and a one letter bug like that could be quite difficult to find if you don't know what you're looking for.

If you're to come back and add or modify these values often, this could become a concern. With the object pattern, it's cleaner and more self-contained.

One potential concern with this solution, however, is if the order in which these providers are looped through is important (i.e. if you always need "yahoo" to output before "gmail"). Currently JavaScript states the following in regards to objects: "It is an unordered collection of properties". This could be subject to change in ES6 (we're currently on ES5). Read more about this particular issue here: Does JavaScript Guarantee Object Property Order?

Community
  • 1
  • 1
Bilal Akil
  • 4,716
  • 5
  • 32
  • 52
0

You will need to eval the isp[x] in order to get the array names of the other two. For example:

var isp = ["yahoo", "gmail"];
var yahoo = ["@yahoo.com", "@rocketmail.com", "@ymail.com"];
var gmail = ["@gmail.com"];

for(x=0;x<isp.length;x++){
    //Should alert 3 Because var yahoo contains 3 element
    //Should alert 1 because var gmail is contain 1 element
    alert(isp[x].length);
    for(y=0;y<eval(isp[x]).length;y++){
        //Should alert @yahoo.com, @rocketmail.com and so on
        alert(eval(isp[x])[y]);
    }
}
Arthur Frankel
  • 4,695
  • 6
  • 35
  • 56
0

This is clear i guess right ? :D

var ispAndData = [
  ["yahoo", ["@yahoo.com", "@rocketmail.com", "@ymail.com"]],
  ["gmail", ["@gmail.com"]]

];

for (x = 0; x < ispAndData.length; x++) {
  //Should alert 3 Because var yahoo contains 3 element
  //Should alert 1 because var gmail is contain 1 element
  document.write(ispAndData[x][0] + " -> ");
  document.write(ispAndData[x][1].length + "</br>");

  for (y = 0; y < ispAndData[x][1].length; y++) {
    //Should alert @yahoo.com, @rocketmail.com and so on
    document.write(ispAndData[x][1][y] + "</br>");
  }
  document.write("</br>");
}
Dula wnRp
  • 163
  • 5