{"id":1026,"date":"2022-08-30T15:11:09","date_gmt":"2022-08-30T15:11:09","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/javascript-length-doesnt-work-on-arrays-that-are-inside-an-object-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:11:09","modified_gmt":"2022-08-30T15:11:09","slug":"javascript-length-doesnt-work-on-arrays-that-are-inside-an-object-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/javascript-length-doesnt-work-on-arrays-that-are-inside-an-object-collection-of-common-programming-errors\/","title":{"rendered":"Javascript: length doesn&#39;t work on arrays that are inside an object?-Collection of common programming errors"},"content":{"rendered":"<p>The main problem is that in javascript data declaration like your first example, the value of <code>this<\/code> is not set to the object being defined. So since <code>this<\/code> is not your object, <code>this.a<\/code> inside the object declaration does not refer to the <code>a<\/code> property of your object.<\/p>\n<p>It is whatever it was in the context right before your data declaration. If that was the global context, then <code>this<\/code> would be the <code>window<\/code> object and <code>this.a<\/code> probably doesn&#8217;t exist. If it doesn&#8217;t exist (e.g. it&#8217;s <code>undefined<\/code>), then when you try to reference <code>this.a.length<\/code>, you are trying to read the property <code>.length<\/code> off something isn&#8217;t an object and that is a javscript error.<\/p>\n<p>As for solving this problem it is generally a bad idea to copy the length of an array to another property. That just gives you a chance for it to be wrong and out-of-sync with the actual length of the array. If you want the length of the array, you should just retrieve it upon demand with:<\/p>\n<pre><code>object.a.length\n<\/code><\/pre>\n<p>If you really want it to be a property (that has a fixed value and doesn&#8217;t change when <code>a<\/code> changes), you can set it after you create the object:<\/p>\n<pre><code>var object = {\n   a:[1,2,3,2],\n};\nobject.b = object.a.length;\nalert(object.b);\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:15:21. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>The main problem is that in javascript data declaration like your first example, the value of this is not set to the object being defined. So since this is not your object, this.a inside the object declaration does not refer to the a property of your object. It is whatever it was in the context [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1026","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1026"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1026\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}