{"id":671,"date":"2022-08-30T15:05:14","date_gmt":"2022-08-30T15:05:14","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/extract-item-from-a-multi-dimensional-array-using-jquery-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:14","modified_gmt":"2022-08-30T15:05:14","slug":"extract-item-from-a-multi-dimensional-array-using-jquery-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/extract-item-from-a-multi-dimensional-array-using-jquery-collection-of-common-programming-errors\/","title":{"rendered":"extract item from a multi dimensional array using jquery-Collection of common programming errors"},"content":{"rendered":"<p>I am using jQuery with jqgrid and I wanted to use few value from the edit results. The result of the edit is a multidimensional array. For some reason I can only alert up to the second child, the result goes <code>undefined<\/code> if i go any further.<\/p>\n<p>The main array variable is <code>postdata<\/code> and if i have to iterate this using jQuery&#8217;s <code>.each()<\/code> function, I get these:<\/p>\n<p>(item : value) format. the number is for the order of appearance<\/p>\n<pre><code>1 readyState : 4\n\n2 setRequestHeader : function({....}) \n..\n..\n..\n21 responseText : Array (\n        [id] =&gt; 364\n        [oper] =&gt; edit\n        [note] =&gt; editing\n        [client] =&gt; raha\n   )\n<\/code><\/pre>\n<p>My intention is to get the value of <code>id<\/code> but if i have to alert the array outside the <code>.each()<\/code> function using this:<\/p>\n<pre><code>alert(postdata['responseText']['id'])\n<\/code><\/pre>\n<p>The alert result is <code>undefined<\/code>. If I removed the <code>['id']<\/code>, it shows the entire <code>responseText<\/code> array (same with number 21).<\/p>\n<p>How do I accomplish this task? many thanks.<\/p>\n<p><strong>SOLVED!<\/strong> Thanks to @Felix for the idea and to @Jasper for suggesting about the <code>console.log<\/code>.<\/p>\n<p>The result of the jqGrid <code>responseText<\/code> is in double quotes, which makes it a string. I managed to solve this using the <code>.split()<\/code> function, two splits to be exact. First splitting it by <code>'\\n'<\/code> and then by <code>'=&gt;'<\/code>. I finally got what I wanted.<\/p>\n<p>Thanks all<\/p>\n<ol>\n<li>\n<p>If you can log the <code>postdata<\/code> variable and post the whole variable it would be much easier to give you advice (<code>console.log(postdata);<\/code>). That being said perhaps <code>responseText<\/code> is an array of objects in which case you would need to access its data like this:<\/p>\n<pre><code>alert(postdata['responseText'][0]['id']);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:21:17. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am using jQuery with jqgrid and I wanted to use few value from the edit results. The result of the edit is a multidimensional array. For some reason I can only alert up to the second child, the result goes undefined if i go any further. The main array variable is postdata and if [&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-671","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/671","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=671"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/671\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}