{"id":795,"date":"2015-01-04T08:59:33","date_gmt":"2015-01-04T16:59:33","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=795"},"modified":"2015-01-04T08:59:33","modified_gmt":"2015-01-04T16:59:33","slug":"creating-a-javascript-array-with-one-to-many-type-relationship","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/creating-a-javascript-array-with-one-to-many-type-relationship\/","title":{"rendered":"Creating a javascript array with one to many type relationship"},"content":{"rendered":"

Sometimes I need to build an array of lists with a one to many type relationship. <\/p>\n

For example this data:
\n1. item1 has a parent of key1.
\n2. item2 has a parent of key2.
\n3. item3 has a parent of key1.<\/p>\n

Using PHP after packing the array I will end up with something like this:<\/p>\n

\r\n<?php\r\n\r\n$m = array();\r\n\r\n$m['key1'][] = 'item1';\r\n$m['key2'][] = 'item2';\r\n$m['key1'][] = 'item3';\r\n\r\nprint_r($m);\r\n\r\n?>\r\n<\/pre>\n

Result:<\/p>\n

\r\nArray\r\n(\r\n    [key1] => Array\r\n        (\r\n            [0] => item1\r\n            [1] => item3\r\n        )\r\n\r\n    [key2] => Array\r\n        (\r\n            [0] => item2\r\n        )\r\n\r\n)\r\n<\/pre>\n

In javascript there is some options to do something similar. I ended up with something like this:
\npackedArr['key1'] = \"item1,item3\"
\npackedArr['key2'] = \"item2\"<\/p>\n

Not exactly array in array[key] but the comma separated list did what I needed.<\/p>\n

Here is the javascript itself. To test it you can use online javascript testers or just make up a html page with a button and maybe have onclick display document.getElementById(\"demo\").innerHTML = output;<\/p>\n

\r\n<script>\r\nfunction packArr() {\r\nvar pA = Array();\r\n\r\nvar inA = [ \r\n  ["child1","parent1"],\r\n  ["child2","parent2"],\r\n  ["child3","parent1"],\r\n  ["child4","parent1"],\r\n  ["child5","parent2"]\r\n  ];\r\n\r\nvar inALen = inA.length;\r\nfor (var i = 0; i < inALen; i++) {\r\n    \/\/alert(inA[i]);\r\n  if (pA[inA[i][1]] == null) {\r\n     pA[inA[i][1]] = inA[i][0];\r\n  } else {\r\n    pA[inA[i][1]] = pA[inA[i][1]] + "," + inA[i][0];\r\n  }\r\n}\r\n\r\nres="";\r\nfor ( var key in pA ) { \r\n  res = res + key + ": " + pA[key] + "<br>";\r\n};\r\n\/\/res = myArray["parent1"]; \r\ndocument.getElementById("demo").innerHTML = res;\r\n}\r\n<\/script>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

Sometimes I need to build an array of lists with a one to many type relationship. For example this data:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55,19],"tags":[],"class_list":["post-795","post","type-post","status-publish","format-standard","hentry","category-javascript","category-php"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/795","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=795"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/795\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}