Skip to content

Instantly share code, notes, and snippets.

@mariuszklinger
Last active January 29, 2020 11:15
Show Gist options
  • Save mariuszklinger/330bbf5d7cd4fccc0f1b127c684c4a74 to your computer and use it in GitHub Desktop.
Save mariuszklinger/330bbf5d7cd4fccc0f1b127c684c4a74 to your computer and use it in GitHub Desktop.

Revisions

  1. mariuszklinger revised this gist Jan 29, 2020. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions infinite-flat.js
    Original file line number Diff line number Diff line change
    @@ -6,13 +6,14 @@ function flat(arr) {

    while (stack.length) {
    const current = stack.shift();
    const isCurrentArray = Array.isArray(current);

    if (Array.isArray(current)) {
    if (isCurrentArray) {
    stack.unshift(...current);
    continue;
    }
    else {
    result.push(current);
    }

    result.push(current);
    }

    return result;
  2. mariuszklinger revised this gist Jan 28, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion infinite-flat.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    const example1 = [[1,1,1,[1.4, 1.6]], 666, [2, 3, [[4], [5, 6]]]];
    const example1 = [[1, 1, 1, [1.4, 1.6]], 666, [2, 3, [[4], [5, 6]]]];

    function flat(arr) {
    const stack = arr;
  3. mariuszklinger revised this gist Jan 28, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions infinite-flat.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    const example1 = [[1,1,1,[1.4, 1.6]], 666, [2, 3, [[4], [5, 6]]]];

    function flat(arr) {
    const stack = arr;
    const result = [];
    const stack = arr;
    const result = [];

    while (stack.length) {
    const current = stack.shift();
    while (stack.length) {
    const current = stack.shift();

    if (Array.isArray(current)) {
    stack.unshift(...current);
  4. mariuszklinger revised this gist Jan 28, 2020. No changes.
  5. mariuszklinger revised this gist Jan 28, 2020. No changes.
  6. mariuszklinger created this gist Jan 28, 2020.
    21 changes: 21 additions & 0 deletions infinite-flat.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    const example1 = [[1,1,1,[1.4, 1.6]], 666, [2, 3, [[4], [5, 6]]]];

    function flat(arr) {
    const stack = arr;
    const result = [];

    while (stack.length) {
    const current = stack.shift();

    if (Array.isArray(current)) {
    stack.unshift(...current);
    }
    else {
    result.push(current);
    }
    }

    return result;
    }

    console.assert(`${flat(example1)}`, '1,1,1,1.4,1.6,666,2,3,4,5,6');