10 examples of 'array.flat' in JavaScript

Every line of 'array.flat' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
16public static flat<T>(array: T[][]): T[] {
17 let count = 0;
18 for (let i = 0; i < array.length; i++) {
19 count += array[i].length;
20 }
21 const ret = new Array<T>(count);
22 count = 0;
23 for (let i = 0; i < array.length; i++) {
24 const ar = array[i];
25 for (let j = 0; j < ar.length; j++) {
26 ret[count] = ar[j];
27 count++;
28 }
29 }
30 return ret;
31}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

11function flat (arr) {
12 return [].concat(...arr)
13}
1function StripToFlat(array) {
2 if (!Array.isArray(array)) {
3 return [array];
4 }
5
6 var res = [];
7
8 for (var i = 0; i < array.length; i++) {
9 var el = array[i];
10
11 if (Array.isArray(el)) {
12 res = res.concat(StripToFlat(el));
13 } else {
14 res.push(el);
15 }
16 }
17
18 return res;
19}
7export function flat<T>(arr: T[][]): T[] {
8 return arr.reduce((a, b) => a.concat(b))
9}
75flat(field = "children", current = true) {
76 const reference = this.data;
77 this.data = Ele.elementFlat(reference, field, current);
78 return this;
79}
3static flatten(arr) { return [].concat.apply([], arr); }
28function flatten(array) { return array.length > 0 ? concat.apply([], array) : array }
57function flatten() {
58 /* convenience for backward compatibility */
59 return new Float32Array(Array.prototype.slice.call(this.flat));
60},
36function flatArray(input) {
37 const flattenedArr = [];
38 function flat(list) {
39 list.forEach(item => {
40 if (Array.isArray(item)) {
41 flat(item)
42 } else {
43 flattenedArr.push(item);
44 }
45 })
46 }
47
48 flat(input);
49 return flattenedArr;
50}
296public static flattenArray<T>(array: (T | T[])[]): T[]
297{
298 const result: T[] = [];
299 for (const item of array)
300 item instanceof Array
301 ? result.push(...Util.flattenArray(item))
302 : result.push(item);
303 return result;
304}

Related snippets