{"id":64393,"date":"2024-08-10T03:01:00","date_gmt":"2024-08-09T17:01:00","guid":{"rendered":"https:\/\/65.254.95.247\/ITblog\/?p=64393"},"modified":"2024-08-10T20:21:07","modified_gmt":"2024-08-10T10:21:07","slug":"javascript-map-array-genericization-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-map-array-genericization-tutorial\/","title":{"rendered":"Javascript Map Array Genericization Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=\"_blank\" href=\"https:\/\/www.rjmprogramming.com.au\/HTMLCSS\/map_test.html\" rel=\"noopener\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Javascript Map Array Genericization Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/map_test_more.gif\" title=\"Javascript Map Array Genericization Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Javascript Map Array Genericization Tutorial<\/p><\/div>\n<p>We think yesterday&#8217;s <a title='Javascript Map Array Primer Tutorial' href='#jmapt'>Javascript Map Array Primer Tutorial<\/a> could benefit from &#8230;<\/p>\n<ul>\n<li>aspects that make the Inventory aspects to the web application feel more like a &#8220;tool&#8221; &#8230;<\/li>\n<li>aspects that make the Inventory aspects to the web application feel more &#8220;generic&#8221;<\/li>\n<\/ul>\n<p> &#8230; and, you may notice, fixes for the way &#8220;Map.groupBy&#8221; functionality does not appear to work on the <font color=blue>mobile platforms<\/font> my iPhone has for web browsers &#8230;<\/p>\n<p><code><br \/>\nvar text=\"\";<br \/>\n<br \/>\nfunction thecall() {<br \/>\nvar kk=0;<br \/>\n<br \/>\n\/\/ Group by ok and low<br \/>\ntext=\"These \" + document.getElementById('topic').innerHTML.toLowerCase() + \"s are Ok: &lt;br&gt;\";<br \/>\ntry {<br \/>\nconst result = Map.groupBy(fruits, myCallback);<br \/>\n<br \/>\n\/\/ Display Results<br \/>\ntry {<br \/>\nfor (let x of result.get(\"ok\")) {<br \/>\n  if (x.name != '' || x.quantity != 0) {<br \/>\n  text += \"\" + x.name + \" \" + x.quantity + \"&lt;br&gt;\";<br \/>\n  }<br \/>\n  if (!m.has(x.name)) {<br \/>\n    m.set(x.name, x.quantity);<br \/>\n  }<br \/>\n}<br \/>\n} catch(ebad) { }<br \/>\ntext += \"&lt;br&gt;These \" + document.getElementById('topic').innerHTML.toLowerCase() + \"s are low: &lt;br&gt;\";<br \/>\ntry {<br \/>\nfor (let x of result.get(\"low\")) {<br \/>\n  if (x.name != '' || x.quantity != 0) {<br \/>\n  text += \"\" + x.name + \" \" + x.quantity + \"&lt;br&gt;\";<br \/>\n  }<br \/>\n  if (!m.has(x.name)) {<br \/>\n    m.set(x.name, x.quantity);<br \/>\n  }<br \/>\n}<br \/>\nconsole.log(result.get(\"ok\"));<br \/>\n} catch(ebad) { }<br \/>\n} <font color=blue>catch(overebad) {<br \/>\ntext=\"These \" + document.getElementById('topic').innerHTML.toLowerCase() + \"s are Ok: &lt;br&gt;\";<br \/>\nfor (kk=0; kk&lt;fruits.length; kk++) {<br \/>\n    if (('' + fruits[kk].quantity).replace('-','').substring(0,1) &gt;= '0' && ('' + fruits[kk].quantity).replace('-','').substring(0,1) &lt;= '9') {<br \/>\n    if (mysimpleCallback(fruits[kk].quantity) == 'ok') {<br \/>\n    \/\/alert(fruits[kk].name);<br \/>\n  text += \"\" + fruits[kk].name + \" \" + fruits[kk].quantity + \"&lt;br&gt;\";<br \/>\n    \/\/alert('2:' + fruits[kk].name);<br \/>\n  if (!m.has(fruits[kk].name)) {<br \/>\n    \/\/alert('3:' + fruits[kk].name);<br \/>\n    m.set(fruits[kk].name, fruits[kk].quantity);<br \/>\n    \/\/alert('4:' + fruits[kk].name);<br \/>\n  }<br \/>\n    }<br \/>\n    }<br \/>\n}<br \/>\ntext += \"&lt;br&gt;These \" + document.getElementById('topic').innerHTML.toLowerCase() + \"s are low: &lt;br&gt;\";<br \/>\nfor (kk=0; kk&lt;fruits.length; kk++) {<br \/>\n    if (('' + fruits[kk].quantity).replace('-','').substring(0,1) &gt;= '0' && ('' + fruits[kk].quantity).replace('-','').substring(0,1) &lt;= '9') {<br \/>\n    if (mysimpleCallback(fruits[kk].quantity) == 'low') {<br \/>\n  text += \"\" + fruits[kk].name + \" \" + fruits[kk].quantity + \"&lt;br&gt;\";<br \/>\n  if (!m.has(fruits[kk].name)) {<br \/>\n    m.set(fruits[kk].name, fruits[kk].quantity);<br \/>\n  }<br \/>\n    }<br \/>\n    }<br \/>\n}<br \/>\n<br \/>\n}<\/font><br \/>\ndocument.getElementById(\"demo\").innerHTML = text;<br \/>\n<br \/>\n}<br \/>\n<\/code><\/p>\n<p>With this in mind, we honed in on a &#8220;topic&#8221; concept, where yesterday&#8217;s &#8220;topic&#8221; would have been &#8220;Fruit&#8221;.  There are two aspects &#8230;<\/p>\n<ol>\n<li>allow a <font color=blue>contenteditable<\/font> way for user to change the displayed (what used to be) <I>hardcoding<\/i> &#8230;<br \/>\n<code><br \/>\n&lt;span title='Double click to be able to enter CSV data' id=topic <font color=blue>contenteditable=true<\/font> onblur=beadjustable(); <font color=purple>ondblclick=askall();<\/font>&gt;<I>Fruit<\/i>&lt;\/span&gt;<br \/>\n<\/code><br \/>\n &#8230; as well as &#8230;<\/li>\n<li>add <font color=purple>ondblclick<\/font> (ie. on double click) means by which a user can use a Javascript prompt window means by which they can enter all the CSV (ie. comma separated values) data for an Inventory application of the user&#8217;s choosing (including a means by which they can also enter the &#8220;topic&#8221; at the same time) &#8230;<br \/>\n<code><br \/>\n<font color=purple>function askall() {<br \/>\n   var delall=false;<br \/>\n   var enterall=prompt('Optionally copy all your CSV (comma separated values) data to apply here (where ; or | can be record delimiters).  Optionally prefix this CSV data with your topic followed by ~`~' + String.fromCharCode(10) + String.fromCharCode(10) + 'Example ...' + String.fromCharCode(10) + 'Fish~`~taylor,300;barramundi,400;perch,100;mullet,234', '');<br \/>\n   if (enterall == null) {  enterall=''; }<br \/>\n   if (enterall.indexOf('~`~') != -1) {  document.getElementById('topic').innerHTML=enterall.split('~`~')[0];  enterall=enterall.replace(enterall.split('~`~')[0] + '~`~', '');  }<br \/>\n   if (enterall.indexOf(',') != -1) {<br \/>\n     var elines=[];<br \/>\n     if (enterall.indexOf(String.fromCharCode(10)) != -1) {<br \/>\n     elines=enterall.split(String.fromCharCode(10));<br \/>\n     } else if (enterall.indexOf(';') != -1) {<br \/>\n     elines=enterall.split(';');<br \/>\n     } else if (enterall.indexOf('|') != -1) {<br \/>\n     elines=enterall.split('|');<br \/>\n     }<br \/>\n     for (var ie=0; ie&lt;elines.length; ie++) {<br \/>\n        if (elines[ie].indexOf('\",\"') != -1 && elines[ie].indexOf('\",\"') &lt; elines[ie].indexOf(',')) {<br \/>\n          if (!delall) {<br \/>\n            delall=true;<br \/>\n            fruits=[];<br \/>\n            m = new Map();<br \/>\n          }<br \/>\n          if (elines[ie].substring(1).replace('-','').substring(0,1) &gt;= '0' && elines[ie].substring(1).replace('-','').substring(0,1) &lt;= '9') {<br \/>\n            addone(elines[ie].split('\",\"')[1].split('\"')[0], elines[ie].substring(1).split('\"')[0]);<br \/>\n          } else {<br \/>\n            addone(elines[ie].substring(1).split('\"')[0], elines[ie].split('\",\"')[1].split('\"')[0]);<br \/>\n          }<br \/>\n        } else if (elines[ie].indexOf('\",') != -1 && elines[ie].indexOf('\",') &lt; elines[ie].indexOf(',')) {<br \/>\n          if (!delall) {<br \/>\n            delall=true;<br \/>\n            fruits=[];<br \/>\n            m = new Map();<br \/>\n          }<br \/>\n          if (elines[ie].substring(1).replace('-','').substring(0,1) &gt;= '0' && elines[ie].substring(1).replace('-','').substring(0,1) &lt;= '9') {<br \/>\n            addone(elines[ie].split('\",')[1].split(',')[0], elines[ie].substring(1).split('\"')[0]);<br \/>\n          } else {<br \/>\n            addone(elines[ie].substring(1).split('\"')[0], elines[ie].split('\",')[1].split(',')[0]);<br \/>\n          }<br \/>\n        } else if (elines[ie].indexOf(',\"') != -1 && elines[ie].indexOf(',\"') == elines[ie].indexOf(',')) {<br \/>\n          if (!delall) {<br \/>\n            delall=true;<br \/>\n            fruits=[];<br \/>\n            m = new Map();<br \/>\n          }<br \/>\n          if (elines[ie].substring(0).replace('-','').substring(0,1) &gt;= '0' && elines[ie].substring(0).replace('-','').substring(0,1) &lt;= '9') {<br \/>\n            addone(elines[ie].split(',\"')[1].split('\"')[0], elines[ie].substring(0).split(',')[0]);<br \/>\n          } else {<br \/>\n            addone(elines[ie].substring(0).split(',')[0], elines[ie].split(',\"')[1].split('\"')[0]);<br \/>\n          }<br \/>\n        } else if (elines[ie].indexOf(',') != -1) {<br \/>\n          if (!delall) {<br \/>\n            delall=true;<br \/>\n            fruits=[];<br \/>\n            m = new Map();<br \/>\n          }<br \/>\n          if (elines[ie].substring(0).replace('-','').substring(0,1) &gt;= '0' && elines[ie].substring(0).replace('-','').substring(0,1) &lt;= '9') {<br \/>\n            addone(elines[ie].substring(0).split(',')[1], elines[ie].substring(0).split(',')[0]);<br \/>\n          } else {<br \/>\n            addone(elines[ie].substring(0).split(',')[0], elines[ie].substring(0).split(',')[1]);<br \/>\n          }<br \/>\n        }<br \/>\n     }<br \/>\n     beadjustable();<br \/>\n   }<br \/>\n}<\/font><br \/>\n<\/code>\n<\/li>\n<\/ol>\n<p> &#8230; in <a target=\"_blank\" href=\"https:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=https:\/\/www.rjmprogramming.com.au\/HTMLCSS\/map_test.html-GETME\" rel=\"noopener\">a changed<\/a> <a target=\"_blank\" href=\"https:\/\/www.rjmprogramming.com.au\/HTMLCSS\/map_test.html-GETME\" rel=\"noopener\">map_test.html &#8220;proof of concept&#8221;<\/a> <a target=\"_blank\" href=\"https:\/\/www.rjmprogramming.com.au\/HTMLCSS\/map_test.html\" rel=\"noopener\">Array and Map Tester<\/a> web application you can <a href='#ifmap'>also try below<\/a>.<\/p>\n<p><!--p>You can also see this play out at WordPress 4.1.1's <a target=\"_blank\" href='\/\/www.rjmprogramming.com.au\/ITblog\/javascript-map-array-genericization-tutorial\/' rel=\"noopener\">Javascript Map Array Genericization Tutorial<\/a>.<\/p-->\n<hr>\n<p id='jmapt'>Previous relevant <a target=\"_blank\" title='Javascript Map Array Primer Tutorial' href='\/\/www.rjmprogramming.com.au\/ITblog\/javascript-map-array-primer-tutorial\/' rel=\"noopener\">Javascript Map Array Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=\"_blank\" href=\"https:\/\/www.rjmprogramming.com.au\/HTMLCSS\/map_test.html\" rel=\"noopener\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Javascript Map Array Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/map_test.jpg\" title=\"Javascript Map Array Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Javascript Map Array Primer Tutorial<\/p><\/div>\n<p>We&#8217;ve got yet another &#8220;map&#8221; idea for you today, coming from the wooooorrrrlllllddd of Javascript clientside data structures, if you like.<\/p>\n<p>We got onto this topic via reading <a target=\"_blank\" title='https:\/\/www.w3schools.com\/js\/tryit.asp?filename=tryjs_map_groupby' href='https:\/\/www.w3schools.com\/js\/tryit.asp?filename=tryjs_map_groupby' rel=\"noopener\">https:\/\/www.w3schools.com\/js\/tryit.asp?filename=tryjs_map_groupby<\/a> and <a target=\"_blank\" title='https:\/\/medium.com\/@sotoer\/your-foreach-example-has-the-wrong-order-of-params-which-you-are-also-demonstrating-in-your-sample-42f5491b604e' href='https:\/\/medium.com\/@sotoer\/your-foreach-example-has-the-wrong-order-of-params-which-you-are-also-demonstrating-in-your-sample-42f5491b604e' rel=\"noopener\">https:\/\/medium.com\/@sotoer\/your-foreach-example-has-the-wrong-order-of-params-which-you-are-also-demonstrating-in-your-sample-42f5491b604e<\/a> which both helped us enormously put together a rudimentary Fruit Inventory web application featuring &#8230;<\/p>\n<ul>\n<li>array with structure<br \/>\n<code><br \/>\n\/\/ Create an Array<br \/>\nconst fruits = [<br \/>\n  {name:\"apples\", quantity:300},<br \/>\n  {name:\"bananas\", quantity:500},<br \/>\n  {name:\"oranges\", quantity:200},<br \/>\n  {name:\"kiwi\", quantity:150}<br \/>\n];<br \/>\n<\/code>\n<\/li>\n<li>map object<br \/>\n<code><br \/>\nvar m = new Map();<br \/>\n<\/code>\n<\/li>\n<li>use of map.set() &#8230;<br \/>\n<code><br \/>\nfunction addone() {<br \/>\n  doadd=true;<br \/>\n  <font color=purple>fruits.push({name:\"\", quantity:0});<\/font><br \/>\n  <font color=blue>m.set('', 0);<\/font><br \/>\n  doadd=false;<br \/>\n  beadjustable();<br \/>\n}<br \/>\n<\/code><br \/>\n &#8230; and Map.groupBy()<br \/>\n<code><br \/>\nfunction thecall() {<br \/>\n\/\/ Group by ok and low<br \/>\n<font color=blue>const result = Map.groupBy(fruits, myCallback);<\/font><br \/>\n<br \/>\n\/\/ Display Results<br \/>\nlet text =\"These fruits are Ok: &lt;br&gt;\";<br \/>\ntry {<br \/>\nfor (let x of result.get(\"ok\")) {<br \/>\n  if (x.name != '' || x.quantity != 0) {<br \/>\n  text += x.name + \" \" + x.quantity + \"&lt;br&gt;\";<br \/>\n  }<br \/>\n  if (!m.has(x.name)) {<br \/>\n    m.set(x.name, x.quantity);<br \/>\n  }<br \/>\n}<br \/>\n} catch(ebad) { }<br \/>\ntext += \"&lt;br&gt;These fruits are low: &lt;br&gt;\";<br \/>\ntry {<br \/>\nfor (let x of result.get(\"low\")) {<br \/>\n  if (x.name != '' || x.quantity != 0) {<br \/>\n  text += x.name + \" \" + x.quantity + \"&lt;br&gt;\";<br \/>\n  }<br \/>\n  if (!m.has(x.name)) {<br \/>\n    m.set(x.name, x.quantity);<br \/>\n  }<br \/>\n}<br \/>\n} catch(ebad) { }<br \/>\ndocument.getElementById(\"demo\").innerHTML = text;<br \/>\n<br \/>\nconsole.log(result.get(\"ok\"));<br \/>\n}<br \/>\n<\/code>\n<\/li>\n<li>contenteditable=true<br \/>\n<code><br \/>\nfunction consolelog(inrec) {<br \/>\n   if (rspan == 0) {<br \/>\n   document.getElementById(\"tdname\").innerHTML=inrec.split('value:')[1].split(' key:')[0].split(' map:')[0];<br \/>\n   document.getElementById(\"tdquantity\").innerHTML=inrec.split('key:')[1].split(' value:')[0].split(' map:')[0];<br \/>\n   rspan=1;<br \/>\n   } else if (inrec.split('value:')[1].split(' key:')[0].split(' map:')[0] == '') {<br \/>\n   tabletds+='&lt;tr&gt;&lt;td <font color=blue>contenteditable=true<\/font> id=tdname' + rspan + ' onblur=fix(this);&gt;' + inrec.split('value:')[1].split(' key:')[0].split(' map:')[0] + '&lt;\/td&gt;&lt;td <font color=blue>contenteditable=true<\/font> id=tdquantity' + rspan + ' onblur=fix(this);&gt;' +  inrec.split('key:')[1].split(' value:')[0].split(' map:')[0] + '&lt;\/td&gt;&lt;\/tr&gt;';<br \/>\n   rspan++;<br \/>\n   } else {<br \/>\n   tabletds+='&lt;tr&gt;&lt;td contenteditable=false id=tdname' + rspan + ' onblur=fix(this);&gt;' + inrec.split('value:')[1].split(' key:')[0].split(' map:')[0] + '&lt;\/td&gt;&lt;td <font color=blue>contenteditable=true<\/font> id=tdquantity' + rspan + ' onblur=fix(this);&gt;' +  inrec.split('key:')[1].split(' value:')[0].split(' map:')[0] + '&lt;\/td&gt;&lt;\/tr&gt;';<br \/>\n   rspan++;<br \/>\n   }<br \/>\n   \/\/alert(inrec);<br \/>\n   if (doadd) {<br \/>\n   fruits.push({name:\"\", quantity:0});<br \/>\n   m.set('', 0);<br \/>\n   tabletds+='&lt;tr&gt;&lt;td <font color=blue>contenteditable=true<\/font> id=tdname' + rspan + ' onblur=fix(this);&gt;&lt;\/td&gt;&lt;td <font color=blue>contenteditable=true<\/font> id=tdquantity' + rspan + ' onblur=fix(this);&gt;0&lt;\/td&gt;&lt;\/tr&gt;';<br \/>\n   rspan++;<br \/>\n   doadd=false;<br \/>\n   }<br \/>\n}<br \/>\n<\/code>\n<\/li>\n<li><font color=purple>array.push()<\/font><\/li>\n<\/ul>\n<p> &#8230; in <a target=\"_blank\" href=\"https:\/\/www.rjmprogramming.com.au\/HTMLCSS\/map_test.html_GETME\" rel=\"noopener\">map_test.html &#8220;proof of concept&#8221;<\/a> <a target=\"_blank\" href=\"https:\/\/www.rjmprogramming.com.au\/HTMLCSS\/map_test.html\" rel=\"noopener\">Array and Map Tester<\/a> web application &#8230;<\/p>\n<p><iframe id=ifmap src=\"https:\/\/www.rjmprogramming.com.au\/HTMLCSS\/map_test.html\" style=\"width:100%;height:800px;\"><\/iframe><\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d64480' onclick='var dv=document.getElementById(\"d64480\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/map\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d64480' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d64393' onclick='var dv=document.getElementById(\"d64393\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/genericization\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d64393' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We think yesterday&#8217;s Javascript Map Array Primer Tutorial could benefit from &#8230; aspects that make the Inventory aspects to the web application feel more like a &#8220;tool&#8221; &#8230; aspects that make the Inventory aspects to the web application feel more &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-map-array-genericization-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[103,354,2537,1839,1533,4870,3362,1783,631,652,745,795,849,997,2135,2821,1208,2005,1319],"class_list":["post-64393","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-array","tag-dom","tag-generic","tag-genericization","tag-get","tag-groupby","tag-hardcoding","tag-inventory","tag-iphone","tag-javascript","tag-map","tag-mobile","tag-object","tag-programming","tag-push","tag-set","tag-structure","tag-tool","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/64393"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=64393"}],"version-history":[{"count":4,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/64393\/revisions"}],"predecessor-version":[{"id":64398,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/64393\/revisions\/64398"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=64393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=64393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=64393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}