{"id":30041,"date":"2023-01-12T23:23:53","date_gmt":"2023-01-12T17:53:53","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/"},"modified":"2023-01-12T23:23:53","modified_gmt":"2023-01-12T17:53:53","slug":"solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/","title":{"rendered":"[Solved] Why is this javascript Array() producing an array with only one element? [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-67825058\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"67825058\" data-parentid=\"67824910\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>As per Sebastian&#8217;s comment: your <code>requestCount<\/code> is almost certainly a string instead of a number:<\/p>\n<pre><code>new Array(4).fill()\n\/\/ Array(4) [ undefined, undefined, undefined, undefined ]\n\nnew Array(\"4\").fill()\n\/\/ Array(1) [ undefined ]\n<\/code><\/pre>\n<p>In fact the second constructor <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/Array\">does something <em>wildly<\/em> different<\/a> from the first one. <code>new Array(int)<\/code> will create an Array with <code>&lt;int&gt;<\/code> empty slots. However, <code>new Array(not-an-int)<\/code> will create an array with the constructor arguments as content, so:<\/p>\n<pre><code>new Array(4)\n\/\/ Array(4) [ &lt;4 empty slots&gt; ]\n\nnew Array(\"4\")\n\/\/ Array(1) [\"4\"]\n<\/code><\/pre>\n<p>So if we call <code>.fill()<\/code> without any argument, we&#8217;re saying &#8220;replace the content for all elements with <code>undefined<\/code>&#8220;, and we have an array containing a single element (the string &#8220;4&#8221;), then we end up with <code>[undefined]<\/code>.<\/p>\n<p>The lesson here is to <em>always<\/em> validate your inputs before you pass them to functions\/constructors that are polymorphic with respect to argument type:<\/p>\n<pre><code>const requestCount = await campaign.methods.getRequestsCount().call();\n\n\/\/ I don't know what that line can return, so verify that there even\n\/\/ _is_ a result. If there is, then we can continue:\n\nconst n = parseInt(requestCount);\n\nif (isNaN(n)) {\n  \/\/ this is a probably something you should have a contingency for\n}\n\n\/\/ and if n _is_ a number, it also needs to make sense as an array length value\nelse if (n &gt; 0) {\n  const mapped = [...Array(n)].map((_,i) =&gt; {\n    \/\/ do something with i\n  });\n}\n<\/code><\/pre>\n<p>But that &#8220;convenient syntax&#8221; using map basically wastes time on creating an array that we&#8217;re then immediately throwing away. That&#8217;s silly: we can achieve the exact same result without immediately invoking garbage collection by using array&#8217;s stack functions:<\/p>\n<pre><code>\/\/ note: let, not const, because we're going to run it down to 0\nlet n = .....\n\n...code that returns early based on bad values of n...\n\nconst mapped = [];\nwhile(n--) {\n  \/\/ since we're running our index backwards, we use unshift(),\n  \/\/ which is like push() but for the start, not end, of an array.\n  mapped.unshift(campaign.methods.requests(n).call());\n}\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Why is this javascript Array() producing an array with only one element? [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] As per Sebastian&#8217;s comment: your requestCount is almost certainly a string instead of a number: new Array(4).fill() \/\/ Array(4) [ undefined, undefined, undefined, undefined ] new Array(&#8220;4&#8221;).fill() \/\/ Array(1) [ undefined ] In fact the second constructor does something wildly different from the first one. new Array(int) will create an Array with &lt;int&gt; empty &#8230; <a title=\"[Solved] Why is this javascript Array() producing an array with only one element? [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/\" aria-label=\"More on [Solved] Why is this javascript Array() producing an array with only one element? [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[361,333],"class_list":["post-30041","post","type-post","status-publish","format-standard","hentry","category-solved","tag-arrays","tag-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Why is this javascript Array() producing an array with only one element? [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Why is this javascript Array() producing an array with only one element? [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] As per Sebastian&#8217;s comment: your requestCount is almost certainly a string instead of a number: new Array(4).fill() \/\/ Array(4) [ undefined, undefined, undefined, undefined ] new Array(&quot;4&quot;).fill() \/\/ Array(1) [ undefined ] In fact the second constructor does something wildly different from the first one. new Array(int) will create an Array with &lt;int&gt; empty ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-12T17:53:53+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Why is this javascript Array() producing an array with only one element? [closed]\",\"datePublished\":\"2023-01-12T17:53:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/\"},\"wordCount\":175,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"arrays\",\"javascript\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/\",\"name\":\"[Solved] Why is this javascript Array() producing an array with only one element? [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-12T17:53:53+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Why is this javascript Array() producing an array with only one element? [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Why is this javascript Array() producing an array with only one element? [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Why is this javascript Array() producing an array with only one element? [closed] - JassWeb","og_description":"[ad_1] As per Sebastian&#8217;s comment: your requestCount is almost certainly a string instead of a number: new Array(4).fill() \/\/ Array(4) [ undefined, undefined, undefined, undefined ] new Array(\"4\").fill() \/\/ Array(1) [ undefined ] In fact the second constructor does something wildly different from the first one. new Array(int) will create an Array with &lt;int&gt; empty ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/","og_site_name":"JassWeb","article_published_time":"2023-01-12T17:53:53+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Why is this javascript Array() producing an array with only one element? [closed]","datePublished":"2023-01-12T17:53:53+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/"},"wordCount":175,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["arrays","javascript"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/","name":"[Solved] Why is this javascript Array() producing an array with only one element? [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-12T17:53:53+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-why-is-this-javascript-array-producing-an-array-with-only-one-element-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Why is this javascript Array() producing an array with only one element? [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/30041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=30041"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/30041\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=30041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=30041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=30041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}