Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, store your rows in an array as well, your ROW1 - ROW4 are going to give you headaches.</p> <pre><code>var ROWS = [ ['module1'], ['module4', 'module2'], ['module5'], ['module7', 'module3', 'module8'] ]; </code></pre> <p>You also want a way to remove an element. <a href="http://wolfram.kriesing.de/blog/index.php/2008/javascript-remove-element-from-array" rel="nofollow noreferrer"><code>.splice()</code></a> is built into arrays, and works great to remove elements.</p> <pre><code>// lets clear out our disabled modules var disabledModules = ['module3', 'module5']; // quick sample function - yours might check for empty content or whatever else... function isDisabled(module) { for (var i=0, test; test=disabledModules[i]; i++) { if (test === module) return true; } return false; } for(var i=0,row; row=ROWS[i]; i++) { for (var j=0,module; module=row[j]; j++) { // is this module in our disabled modules list? if (isDisabled(module)) { // the module is disabled row.splice(j,1); j--; // so we recheck this point in the array } } if (row.length &lt; 1) { // all items were deleted ROWS.splice(i,1); i--; // so we recheck this point.. } } // ROWS === [["module1"], ["module4", "module2"], ["module7", "module8"]] </code></pre> <p>Your comment asked about the for loop syntax used. It's a pretty quick way to loop through arrays you know won't have "false" values. Breaking one apart and turning on verbose logging:</p> <pre><code> for( // Part 1 of for loop: Setup, create two variables, i=0, and row. var i=0,row; // Part 2: The test - when false, loop will exit, gets executed pre-loop every time // Assignment operator returns the value assigned, so row=ROWS[i] will be undefined // (false) when we reach then end of the array. row=ROWS[i]; // Part 3: Post loop - increment i i++) { </code></pre> <p>The dangers of using this method do exist though, if its an array of numbers for instance, and one of those numbers is 0, the loop will exit early. The alternative would be to write:</p> <pre><code>for(var i=0; i&lt;ROWS.length; i++) { var row = ROWS[i]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload