Home > Enterprise >  need help in nested array concatenaton/appending operation concept
need help in nested array concatenaton/appending operation concept

Time:01-04

i have three arrays. H, E, and F.

const H = [
    [$, _, _, _, _, _, _, $,],
    [$, _, _, _, _, _, _, $,],
    [$, _, _, _, _, _, _, $,],
    [$, _, _, _, _, _, _, $,],
    [$, _, _, _, _, _, _, $,],
    [$, _, _, _, _, _, _, $,],
    [$, $, $, $, $, $, $, $,],
    [$, _, _, _, _, _, _, $,],
    [$, _, _, _, _, _, _, $,],
    [$, _, _, _, _, _, _, $,],
    [$, _, _, _, _, _, _, $,],
    [$, _, _, _, _, _, _, $,],
    [$, _, _, _, _, _, _, $,],
]

const E = [
    [$, $, $, $, $, $, $, $,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, $, $, $, $, $, $, $,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, $, $, $, $, $, $, $,],
]

const F = [
    [$, $, $, $, $, $, $, $,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, $, $, $, $, $, $, $,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, _,],
  ];

i want following output: H E F which is:

[
    [$, _, _, _, _, _, _, $, $, $, $, $, $, $, $, $, $, $, $, $, $, $, $, $,],
    [$, _, _, _, _, _, _, $, $, _, _, _, _, _, _, _, $, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, $, $, _, _, _, _, _, _, _, $, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, $, $, _, _, _, _, _, _, _, $, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, $, $, _, _, _, _, _, _, _, $, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, $, $, _, _, _, _, _, _, _, $, _, _, _, _, _, _, _,],
    [$, $, $, $, $, $, $, $, $, $, $, $, $, $, $, $, $, $, $, $, $, $, $, $,],
    [$, _, _, _, _, _, _, $, $, _, _, _, _, _, _, _, $, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, $, $, _, _, _, _, _, _, _, $, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, $, $, _, _, _, _, _, _, _, $, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, $, $, _, _, _, _, _, _, _, $, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, $, $, _, _, _, _, _, _, _, $, _, _, _, _, _, _, _,],
    [$, _, _, _, _, _, _, $, $, $, $, $, $, $, $, $, $, _, _, _, _, _, _, _,],
]

basically i want to combine HEF from H,E,F. any solution through looping or maybe if lodash provide this functionality? as this is array in array im open to looping solutions and any lodash or maybe if any javascript library provide this functionality then let me know.

CodePudding user response:

Looks like you just need to apply concat to each row separately:

a = [
    ['a','b','c'],
    ['d','e','f'],
]
b = [
    ['g','h'],
    ['i','j'],
]
c = [
    ['k', 'l'],
    ['m', 'n'],
]


let glue = args => args[0].map(
    (_, i) => [].concat(
        ...args.map(a => a[i])))

result = glue([a, b, c])

console.log(result)

CodePudding user response:

You could take an object for the letters and map rows.

const
    getText = (text, data) => Array
        .from(text)
        .reduce((r, c) => data[c].map((a, i) => [...r[i] || [], ...a]), []),
    _ = '_',
    $ = '$',
    H = [[$, _, _, _, _, _, _, $], [$, _, _, _, _, _, _, $], [$, _, _, _, _, _, _, $], [$, _, _, _, _, _, _, $], [$, _, _, _, _, _, _, $], [$, _, _, _, _, _, _, $], [$, $, $, $, $, $, $, $], [$, _, _, _, _, _, _, $], [$, _, _, _, _, _, _, $], [$, _, _, _, _, _, _, $], [$, _, _, _, _, _, _, $], [$, _, _, _, _, _, _, $], [$, _, _, _, _, _, _, $]],
    E = [[$, $, $, $, $, $, $, $], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, $, $, $, $, $, $, $], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, $, $, $, $, $, $, $]],
    F = [[$, $, $, $, $, $, $, $], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, $, $, $, $, $, $, $], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _], [$, _, _, _, _, _, _, _]];
    
getText('HEF', { E, F, H }).map(a => console.log(...a));
.as-console-wrapper { max-height: 100% !important; top: 0; }

  • Related