Building 2 dimensional array

List A
Canon
Nikon
Samsung
Sony
List B
Olympus
Pentax
Lumix
Leica

The code extracts items from lists above and creates an array with 2 items (2 lists) that each contain items within that list.


            // Array looks like this:
            tables = [["Canon", "Nikon", "Samsung", "Sony"], ["Olympus", "Pentax", "Lumix", "Leica"]]
            


Result - jquery

            // with jQuery
            $(document).ready(function(){

                // Build 2-dimensional array
                // Building an array that looks like this:
                // tables = [["Canon", "Nikon", "Samsung", "Sony"], ["Olympus", "Pentax", "Lumix", "Leica"]]

                var tables = [];  // define parent array

                $(".restable").each(function(i){
                    var tblName = $(this).attr("id");

                    tables[i] = []; // define nested array

                    $(this).children().each(function(j){
                        var labelName = $(this).text();
                        tables[i][j] = labelName; // populate nested array
                    });
                });

                $("#pbt1").append("<p>Table 1 contains: " + tables[0] + "<br>" + "Table 2 contains: " + tables[1] + "</p>");
            });
        


Result - javascript

            // with raw javascript
            function getElemByClass(className, tag, elm){
                var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
                var tag = tag || "*";
                var elm = elm || document;
                var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
                var returnElements = [];
                var current;
                var length = elements.length;
                for(var i=0; i < length; i++){
                    current = elements[i];
                    if(testClass.test(current.className)){
                        returnElements.push(current);
                    }
                }
                return returnElements;
            }

            var tableArray =[];
            var outerDiv = getElemByClass("restable");

            for(var i=0; i < outerDiv.length; i++){
                var outerDivName = outerDiv[i].id;
                tableArray[i] = [];

                var innerDiv = outerDiv[i].children;
                for(var j=0; j < innerDiv.length; j++){
                    var innerDivTxt = innerDiv[j].innerHTML;
                    tableArray[i][j] = innerDivTxt;
                }
            }

            var myDiv = document.getElementById("pbt2");
            var newNode = document.createElement('p');
            newNode.innerHTML = "Table 1 contains: " + tableArray[0] + "<br>" + "Table 2 contains: " + tableArray[1];
            myDiv.appendChild(newNode);