Flatten array of arrays in TypeScript
Try this:
const a = [["a", "b", "c"], ["a", "b", "c"], ["a", "b", "c"]]const result = a.reduce((accumulator, value) => accumulator.concat(value), []);console.log(result)
You can flatten the array with flat()
let foo: string[][] = [["a", "b", "c"], ["a", "b", "c"], ["a", "b", "c"]];let bar = foo.flat()
log
console.log(bar) // a,b,c,a,b,c,a,b,c
UPDATE
By correcting the type to string[] you can also use concat
let foo: string[][] = [["a", "b", "c"], ["a", "b", "c"], ["a", "b", "c"]];let bar : string[] = []bar = bar.concat(foo[0], foo[1], foo[2])
Here's the simplest option:
let bar = ([] as string[]).concat(...foo);
Like @Kokodoko's approach but with the typings inline.