{"id":3568,"date":"2020-03-05T13:36:51","date_gmt":"2020-03-05T12:36:51","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=3568"},"modified":"2023-04-04T14:01:55","modified_gmt":"2023-04-04T13:01:55","slug":"javascript-cartesian-product","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/javascript-cartesian-product\/","title":{"rendered":"Javascript Cartesian Product"},"content":{"rendered":"\n<p>(Returns all combinations for one or more arrays)<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function* cartesian(arrays) {\n\tlet remainder = arrays.length > 1 ? cartesian(arrays.slice(1)) : [[]];\n\tfor (let r of remainder)\n\t\tfor (let h of arrays[0])\n\t\t\tyield [h, ...r];\n}\n\nconst data = Array.from(cartesian([\n\t['A', 'B', 'C'],\n\t[1, 2, 3, 4],\n\t['I', 'II']\n]));\n\nconsole.log(data);\ndata.sort();\nconsole.log(data);<\/pre>\n\n\n\n<p>In Typescript<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"typescript\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">function* cartesian&lt;T>(arrays: T[][]): Generator&lt;T[]> {\n  const remainder = arrays.length > 1 ? cartesian(arrays.slice(1)) : [[]];\n  for (let r of remainder) {\n    for (let h of arrays[0]) {\n      yield [h, ...r];\n    }\n  }\n}<\/pre><\/div>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[ [ 'A', 1, 'I' ],\n  [ 'B', 1, 'I' ],\n  [ 'C', 1, 'I' ],\n  [ 'A', 2, 'I' ],\n  [ 'B', 2, 'I' ],\n  [ 'C', 2, 'I' ],\n  [ 'A', 3, 'I' ],\n  [ 'B', 3, 'I' ],\n  [ 'C', 3, 'I' ],\n  [ 'A', 4, 'I' ],\n  [ 'B', 4, 'I' ],\n  [ 'C', 4, 'I' ],\n  [ 'A', 1, 'II' ],\n  [ 'B', 1, 'II' ],\n  [ 'C', 1, 'II' ],\n  [ 'A', 2, 'II' ],\n  [ 'B', 2, 'II' ],\n  [ 'C', 2, 'II' ],\n  [ 'A', 3, 'II' ],\n  [ 'B', 3, 'II' ],\n  [ 'C', 3, 'II' ],\n  [ 'A', 4, 'II' ],\n  [ 'B', 4, 'II' ],\n  [ 'C', 4, 'II' ] ]\n\n[ [ 'A', 1, 'I' ],\n  [ 'A', 1, 'II' ],\n  [ 'A', 2, 'I' ],\n  [ 'A', 2, 'II' ],\n  [ 'A', 3, 'I' ],\n  [ 'A', 3, 'II' ],\n  [ 'A', 4, 'I' ],\n  [ 'A', 4, 'II' ],\n  [ 'B', 1, 'I' ],\n  [ 'B', 1, 'II' ],\n  [ 'B', 2, 'I' ],\n  [ 'B', 2, 'II' ],\n  [ 'B', 3, 'I' ],\n  [ 'B', 3, 'II' ],\n  [ 'B', 4, 'I' ],\n  [ 'B', 4, 'II' ],\n  [ 'C', 1, 'I' ],\n  [ 'C', 1, 'II' ],\n  [ 'C', 2, 'I' ],\n  [ 'C', 2, 'II' ],\n  [ 'C', 3, 'I' ],\n  [ 'C', 3, 'II' ],\n  [ 'C', 4, 'I' ],\n  [ 'C', 4, 'II' ] ]\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>(Returns all combinations for one or more arrays) In Typescript Output:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,4,1],"tags":[],"class_list":["post-3568","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=3568"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3568\/revisions"}],"predecessor-version":[{"id":7658,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3568\/revisions\/7658"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=3568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=3568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=3568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}