{"id":2159,"date":"2022-08-30T15:22:32","date_gmt":"2022-08-30T15:22:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/04\/creating-multiple-levels-of-json-from-loop-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:32","modified_gmt":"2022-08-30T15:22:32","slug":"creating-multiple-levels-of-json-from-loop-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/creating-multiple-levels-of-json-from-loop-collection-of-common-programming-errors\/","title":{"rendered":"Creating multiple levels of json from loop-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m writing a directive in angular to help validate fields. It is working up to the point of storing the result of the validation.<\/p>\n<p>I want to store it in an object like <code>error.password.old<\/code><\/p>\n<p>the code to do this looks like this:<\/p>\n<pre><code>if(!(typeof iAttrs.ngModel == \"undefined\"))\n{\n    var model = iAttrs.ngModel.split('.');\n    var length = model.length;\n    var target = scope.error;\n\n    if(typeof validation[iAttrs.validate] == \"function\")\n    {\n       for(var index = 0; index &lt; length; index++)\n       {\n           target = target[model[index]];\n       }\n       target = validation[iAttrs.validate]();\n       scope.$apply();\n    }\n    else throw(\"function \" + iAttrs.validate + \" does not exist.\");\n }\n<\/code><\/pre>\n<p>iAttrs.ngModel is holds &#8220;password.old&#8221;.<\/p>\n<p>Google is currently throwing:<\/p>\n<blockquote>\n<p>Uncaught TypeError: Cannot read property &#8216;old&#8217; of undefined<\/p>\n<\/blockquote>\n<p>This is thrown on the second iteration of the for loop.<\/p>\n<p>I understand that it is undefined but i need to find a way to define it on the fly without the normal {} notation.<\/p>\n<p id=\"rop\"><small>Originally posted 2014-01-04 02:48:56. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m writing a directive in angular to help validate fields. It is working up to the point of storing the result of the validation. I want to store it in an object like error.password.old the code to do this looks like this: if(!(typeof iAttrs.ngModel == &#8220;undefined&#8221;)) { var model = iAttrs.ngModel.split(&#8216;.&#8217;); var length = model.length; [&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-2159","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2159","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=2159"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2159\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}