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)
Answer from Ghoul Ahmed on Stack OverflowTry 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])
How should I correctly type an array flatten implementation?
There is no perfect way to type this currently, predominantly because recursive types are not allowed. If we could use recursive types, then the solution would be simple:
type Flattened<T> = T extends Array<infer U> ? Flattened<U> : T;
export const flatten = <T>(arr: T[]): Flattened<T>[] => (
arr.reduce(
(acc, val) => (
Array.isArray(val) ? acc.concat(flatten(val)) : acc.concat(val)
),
[],
)
);In general, when a typing would be easily achieved via recursive types, overloads are the (somewhat nasty) solution. Although they obviously will not be able to type the entire domain of the function, we can write overloads for a reasonable sub-domain. For example, in this case we may want to type an array argument of depth up to 3. This can be done with generics:
function flatten<T>(arr: T[][][]): T[]
function flatten<T>(arr: T[][]): T[]
function flatten<T>(arr: T[]): T[]
function flatten<T>(arr: T[]): T[] {
return arr.reduce(
(acc, val) => (
Array.isArray(val) ? acc.concat(flatten(val)) : acc.concat(val)
),
[],
);
}
const depth1 = flatten([1, 2]); // number[]
const depth2 = flatten([[1], [2]]); // number[]
const depth3 = flatten([[[1]], [[2]]]); // number[]A slightly subtle point is that the overloaded function definitions are matched from top-to-bottom, so we need to have the deepest definition first. All of the uses of the function would match the final (bottom) overload, however, T would end up being an array-type itself. Hopefully it's clear how this approach could be extended to allow deeper array forms to be used.
This is a relatively frequently used approach - see for example the type definitions for Ramda's compose
Edit: The above approach assumes that all the elements of the array argument have the same depth. If this is not true then you can adjust the types slightly to account for this, e.g.
function flatten<T>(arr: (T[][] | T[] | T)[]): T[]
function flatten<T>(arr: (T[] | T)[]): T[]
function flatten<T>(arr: T[]): T[]
function flatten<T>(arr: T[]): T[] {
return arr.reduce(
(acc, val) => (
Array.isArray(val) ? acc.concat(flatten(val)) : acc.concat(val)
),
[],
);
}
const depthVariable = flatten([[[1]], [2], 3]); // number[] More on reddit.com Merge/flatten an array of arrays - javascript
Flatten an array type to string literals
How to loop through an array of arrays and flatten them
Videos
You can use "join()" and "split()":
let arrs = [
["$6"],
["$12"],
["$25"],
["$25"],
["$18"],
["$22"],
["$10"]
];
let newArr = arrs.join(",").split(",");
console.log(newArr); // ["$6", "$12", "$25", "$25", "$18", "$22", "$10"]
In addition, you can use "toString()" and "split()" as well:
let arrs = [
["$6"],
["$12"],
["$25"],
["$25"],
["$18"],
["$22"],
["$10"]
];
let newArr = arrs.toString().split(",");
console.log(newArr); // ["$6", "$12", "$25", "$25", "$18", "$22", "$10"]
However, both two ways above don't work properly if string contains commas:
"join()" and "split()":
let arrs = [
["$,6"],
["$,12"],
["$2,5"],
["$2,5"],
[",$18"],
["$22,"],
["$,1,0"]
];
let newArr = arrs.join(",").split(",");
console.log(newArr);
// ["$", "6", "$", "12", "$2", "5", "$2", "5", "", "$18", "$22", "", "$", "1", "0"]
"toString()" and "split()":
let arrs = [
["$,6"],
["$,12"],
["$2,5"],
["$2,5"],
[",$18"],
["$22,"],
["$,1,0"]
];
let newArr = arrs.toString().split(",");
console.log(newArr);
// ["$", "6", "$", "12", "$2", "5", "$2", "5", "", "$18", "$22", "", "$", "1", "0"]
That's not hard, just iterate over the arrays and merge them:
var result = [], input = [["$6"], ["$12"], ["$25"], ["$25"], ["$18"]];
for (var i = 0; i < input.length; ++i) {
result = result.concat(input[i]);
}