{"id":4412,"date":"2021-01-04T15:00:36","date_gmt":"2021-01-04T14:00:36","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=4412"},"modified":"2022-07-25T08:35:41","modified_gmt":"2022-07-25T07:35:41","slug":"typescript-javascript-full-outer-join","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/typescript-javascript-full-outer-join\/","title":{"rendered":"Typescript \/ javascript full outer join"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const filterUnique = () => {\n    const keys = new Set&lt;string>();\n    return (key: string) => !keys.has(key) ? keys.add(key) || true : false;\n};\n\nfunction* fullOuterJoin&lt;L, R, S>(left: L[], right: R[], keyFn: (a: any) => string, selectFn: (left: L, right: R, key: string) => S) {\n    const leftLookup: { [key: string]: L } = {};\n    const rightLookup: { [key: string]: R } = {};\n    left.forEach(x => leftLookup[keyFn(x)] = x);\n    right.forEach(x => rightLookup[keyFn(x)] = x);\n    const unique = filterUnique();\n    const keys = [...left.map(x => keyFn(x)), ...right.map(x => keyFn(x))].filter(unique);\n    for (const key of keys) {\n        const l = leftLookup[key];\n        const r = rightLookup[key];\n        yield selectFn(l, r, key);\n    }\n};\n\nconst left = [1, 2, 3, 4];\nconst right = [3, 4, 5, 6];\n\nfor (const item of fullOuterJoin(left, right, (a) => a, (l: number, r: number, k: string) => ({ l, r }))) {\n    console.log('item', item);\n}\n\nconst flattenObject = (obj: object): any => {\n    const result = {};\n    for (const [k, v] of Object.entries(obj)) {\n        if (typeof v === \"object\" &amp;&amp; v) {\n            for (const [x, y] of Object.entries(flattenObject(v))) {\n                result[k + \".\" + x] = y;\n            }\n            continue;\n        }\n        result[k] = v;\n    }\n    return result;\n};\n\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","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-4412","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\/4412","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=4412"}],"version-history":[{"count":3,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4412\/revisions"}],"predecessor-version":[{"id":4421,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4412\/revisions\/4421"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=4412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=4412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=4412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}