/    Sign up×
Community /Pin to ProfileBookmark

Use of map() function

I’m trying to understand the use of the map() function using multi-dimensional arrays.
Below is my attempt to create a temporary display table, but I’m having two questions.
1. Why are there a line of commas (,,,,,,,,,,) after the table display?
2. Why is the first column so much wider that the other cells?

[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″ />
<title> Event Binding </title>
<style>
#tbl { width: 50%; } /* border: 1px solid red; } */
table {
border-collapse: collapse;
border-spacing: 0;
border: 1px solid #ddd;
}
th, td {
border: 1px solid black;
text-align: right;
padding: 2px;
}
tr:nth-child(even){background-color: #d2d2d2; }
</style>
</head>
<body>
<h2> Table Generation </h2>

<table id=”tbl”></table>

<script type=”text/javascript”>
var tableContents = [ [1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20] ];

(function () {

function bindHandlers(){
var tbl = ”, arr = [];
for (var i=0; i<tableContents.length; i++) {
tmp = tableContents[i].map(
function mapper(elm, index){ return ‘<td>’+elm+’:’+index+'</td>’ } );
arr.push(‘<tr>’+tmp+'</tr>’);
} return arr;
}
document.getElementById(‘tbl’).innerHTML = bindHandlers()

})();
</script>

</body>
</html>

[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumDec 22.2018 — Arrays are not an appropriate way to do this task: There is array to string conversion necessary resulting in the commas being output.

Simply create the HTML as a string:
function bindHandlers() {
var thehtml = '';
tableContents.forEach(function (ele1, idx1) {
thehtml += '&lt;tr&gt;';
ele1.forEach(function (ele2, idx2) {
thehtml += '&lt;td&gt;' + ele2 + ':' + idx2 + '&lt;/td&gt;';
});
thehtml += '&lt;/tr&gt;';
});
return thehtml;
}
document.getElementById('tbl').innerHTML = bindHandlers()
Copy linkTweet thisAlerts:
@JMRKERauthorDec 22.2018 — Thanks for the reply.

Did not really answer the questions, but your output is definitely better than my feeble attempts.

I see what you are doing, but I was also interested in knowing what I was doing wrong.

Is the second 'foreach' statement removing the commas?

Were the commas causing the first column to be extra wide?

Where were the commas coming from to begin with? The map. function or the array.push statement?

I was trying to understand the 'map' function better to see where it could/should be used,

but your example is good to learn from, and perhaps better because it does produce a nice result.
Copy linkTweet thisAlerts:
@SempervivumDec 22.2018 — The commas are created when an array is converted to a string. Try this demo:
var arrcells = ['&lt;td&gt;0:5&lt;/td&gt;', '&lt;td&gt;0:5&lt;/td&gt;'];
var therow = '&lt;tr&gt;' + arrcells + '&lt;/tr&gt;';
console.log(therow);

This will output:

<tr><td>0:5</td>,<td>0:5</td></tr>

A comma between two table cells is not valid and will cause unpredictable results, here the commas being displayed below the table.
Copy linkTweet thisAlerts:
@SempervivumDec 22.2018 — PS: You can can convert the arrays to strings without commas by using the function join():

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
Copy linkTweet thisAlerts:
@JMRKERauthorDec 23.2018 — Thanks 'Sempervivum', with your persistent suggestions, I finally figured out where the extra commas were coming from. I needed to add tmp.join('') and arr.join('') to the original post code to see the display I expected using the map() function. Before the first post I had tried using .join() without the '' characters: .join(''), which caused the commas to be included into the display string.

Thanks for taking the time.

Revised code from post #1.

<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8" /&gt;
&lt;title&gt; Event Binding &lt;/title&gt;
&lt;style&gt;
#tbl { width: 25%; } /* border: 1px solid red; } */
table {
border-collapse: collapse;
border-spacing: 0;
border: 1px solid #ddd;
}
th, td {
border: 1px solid black;
text-align: right;
padding: 2px;
}
tr:nth-child(even){background-color: #d2d2d2; }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h2&gt; Table Generation &lt;/h2&gt;

&lt;table id="tbl"&gt;&lt;/table&gt;

&lt;script type="text/javascript"&gt;
var tableContents = [ [1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20] ];

(function () {

function bindHandlers(){
var tbl = '', arr = [];
for (var i=0; i&lt;tableContents.length; i++) {
tmp = tableContents[i].map(
function mapper(elm, index){ return '&lt;td&gt;'+elm+':'+index+'&lt;/td&gt;' } );
arr.push('&lt;tr&gt;'+tmp.join('')+'&lt;/tr&gt;');
} return arr.join('');
}
document.getElementById('tbl').innerHTML = bindHandlers()

})();
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...