{"id":4081,"date":"2022-12-12T09:56:28","date_gmt":"2022-12-12T09:56:28","guid":{"rendered":"https:\/\/info.documotor.com\/?page_id=4081"},"modified":"2023-01-25T10:19:33","modified_gmt":"2023-01-25T10:19:33","slug":"list_join","status":"publish","type":"page","link":"https:\/\/info.documotor.com\/?page_id=4081","title":{"rendered":"List_join"},"content":{"rendered":"\n<p>The list_join function can be used to group the elements of two arrays and categorize them into objects, based on an expression from each array. <\/p>\n\n\n\n<h3>Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">array list_join(array $leftArray, array $rightArray, &amp;leftEvaluateBy, &amp;rightEvaluateBy)<\/code><\/pre>\n\n\n\n<p>Groups the items of $leftArray and $rightArray base on the corresponding expressions (&amp;leftEvaluateBy and &amp;rightEvaluateBy) into an object. The collection of the generated objects is then enclosed in an array.<\/p>\n\n\n\n<h3>Parameters<\/h3>\n\n\n\n<p><strong>$leftArray:<\/strong> One of the arrays that are to be joined.<br><strong>$rightArray:<\/strong> The other array that will be joined with $leftArray.<br><strong>&amp;leftEvaluateBy:<\/strong> The expression that will be used for the evaluation of $leftArray.<br><strong>&amp;rightEvaluateBy<\/strong>:<strong> <\/strong>The expression that will be used for the evaluation of $rightArray.<\/p>\n\n\n\n<h3>Example<\/h3>\n\n\n\n<p>In the data set below, there are two arrays that will be joined using the list_join function. As it can be seen in the transformation block, the expressions that are used to evaluate each array are &#8220;id&#8221; and &#8220;Dep_id&#8221; respectively. Notice that the keys used for the expression don&#8217;t have to have the same name, as only their values are used for the evaluation.<\/p>\n\n\n\n<pre title=\"Data\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"Names\": [\n        {\n            \"id\": 1,  \/\/This will be used to evaluate the \"Names\" array\n            \"name\": \"James\"\n        },\n        {\n            \"id\": 2,\n            \"name\": \"John\"\n        },\n        {\n            \"id\": 2,\n            \"name\": \"Rita\"\n        },\n        {\n            \"id\": 3,\n            \"name\": \"Matilda\"\n        },\n        {\n            \"id\": 4,\n            \"name\": \"Alison\"\n        },\n        {\n            \"id\": 4,\n            \"name\": \"Alex\"\n        }\n    ],\n    \"Team\": [\n        {\n            \"Dep_id\": 1,  \/\/This will be used to evaluate the \"Team\" array\n            \"Department\": \"HR\"\n        },\n        {\n            \"Dep_id\": 4,\n            \"Department\": \"Project Management\"\n        },\n        {\n            \"Dep_id\": 2,\n            \"Department\": \"Development\"\n        }\n    ]\n}<\/code><\/pre>\n\n\n\n<pre title=\"Transformation\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">{\n    LJ: list_join(Names, Team, &amp;id, &amp;Dep_id)\n}<\/code><\/pre>\n\n\n\n<p>As seen in the output data below, the function returns a joined list of objects, where each object contains an index key, the entries from the $leftArray as well as the entries from the $rightArray that match the evaluation criteria.<\/p>\n\n\n\n<p>In this case, all objects from the &#8220;Names&#8221; array ($leftArray) where the value of &#8220;id&#8221; is equal to the value of &#8220;Dep_id&#8221; (from the &#8220;Team&#8221; array) are collected in the &#8220;left&#8221; array of each object. Similarly, all objects from the &#8220;Team&#8221; array ($rightArray) where the &#8220;Dep_id&#8221; value matches the value of &#8220;id&#8221; from the &#8220;Names&#8221; array are gathered in the &#8220;right&#8221; array of each object. <\/p>\n\n\n\n<pre title=\"Output\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"LJ\": [\n        {\n            \"left\": [  \/\/Contains the items from the $leftArray for which the value of \"id\" is equal to the value of \"Dep_id\"\n                {\n                    \"id\": 1,\n                    \"name\": \"James\"\n                }\n            ],\n            \"right\": [  \/\/Contains the items from the $rigthArray for which the value of \"Dep_id\" is equal to the value of \"id\"\n                {\n                    \"Dep_id\": 1,\n                    \"Department\": \"HR\"\n                }\n            ],\n            \"__index\": 0 \/\/Key generated by function\n        },\n        {\n            \"left\": [\n                {\n                    \"id\": 2,\n                    \"name\": \"John\"\n                },\n                {\n                    \"id\": 2,\n                    \"name\": \"Rita\"\n                }\n            ],\n            \"right\": [\n                {\n                    \"Dep_id\": 2,\n                    \"Department\": \"Development\"\n                }\n            ],\n            \"__index\": 1\n        },\n        {\n            \"left\": [\n                {\n                    \"id\": 4,\n                    \"name\": \"Alison\"\n                },\n                {\n                    \"id\": 4,\n                    \"name\": \"Alex\"\n                }\n            ],\n            \"right\": [\n                {\n                    \"Dep_id\": 4,\n                    \"Department\": \"Project Management\"\n                }\n            ],\n            \"__index\": 2\n        },\n        {\n            \"left\": [\n                {\n                    \"id\": 3,\n                    \"name\": \"Matilda\"\n                }\n            ],\n            \"right\": [], \/\/No objects with matching id in the \"Team\" array\n            \"__index\": 3\n        }\n    ]\n}<\/code><\/pre>\n\n\n\n<p>The &#8220;__index&#8221; key, is generated by the function and it can be useful to target a particular object of the generated array.<\/p>\n\n\n\n<p>As can be noticed in the generated array, in cases where objects from one of the original arrays can&#8217;t be matched to objects in the other, they are still displayed, but they are placed last. Naturally, one of the two arrays of the object (&#8220;left&#8221; or &#8220;right&#8221;) will be empty.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The list_join function can be used to group the elements of two arrays and categorize them into objects, based on an expression from each array. Syntax Groups the items of $leftArray and $rightArray base on the corresponding expressions (&amp;leftEvaluateBy and &amp;rightEvaluateBy) into an object. The collection of the generated objects is then enclosed in an [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"parent":271,"menu_order":110,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/4081"}],"collection":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4081"}],"version-history":[{"count":9,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/4081\/revisions"}],"predecessor-version":[{"id":4184,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/4081\/revisions\/4184"}],"up":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/271"}],"wp:attachment":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}