In the same vein as MamaWalter's answer, here is an alternative functional approach that uses DOM nodes rather than string concatenation:
var tab = document.getElementById('tab');
var bind = Function.prototype.bind,
$tr = bind.call(Document.prototype.createElement, document, "tr"),
$td = bind.call(Document.prototype.createElement, document, "td"),
$text = bind.call(Document.prototype.createTextNode, document),
appender = function(parent, child) {
parent.appendChild(child);
return parent;
},
wrapWith = function(fn, child) { return appender(fn(), child); },
wrapWithTd = wrapWith.bind(undefined, $td);
var outer = [1,2,3], inner = [1,2,3];
outer.map(function(row) {
function createCellContents(col) {
return $text(row+ '' + col);
}
return inner
.map(createCellContents)
.map(wrapWithTd)
.reduce(appender, $tr());
}).reduce(appender, tab);
jsbin for reference.
source
share