Skip to content

Instantly share code, notes, and snippets.

@tobiaslins
Last active March 3, 2017 13:16
Show Gist options
  • Select an option

  • Save tobiaslins/46c512f5bd65631fe38e6091cbbd8099 to your computer and use it in GitHub Desktop.

Select an option

Save tobiaslins/46c512f5bd65631fe38e6091cbbd8099 to your computer and use it in GitHub Desktop.

Revisions

  1. tobiaslins revised this gist Mar 3, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions flatarray.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    const testArray = [[1,2,[3]],4]

    const flat = (arr) => arr.reduce((a, b) => {
    const flatArray = (arr) => arr.reduce((a, b) => {
    const newValue = Array.isArray(b) ? flat(b) : b
    return a.concat(newValue)
    }, [])

    console.log(flat(testArray))
    console.log(flatArray(testArray))
  2. tobiaslins revised this gist Mar 3, 2017. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions flatarray.js
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,8 @@
    const testArray = [[1,2,[3]],4]

    const flat = (arr) => {
    return arr.reduce((a, b) => {
    const newValue = Array.isArray(b) ? flat(b) : b
    return a.concat(newValue)
    }, [])
    }
    const flat = (arr) => arr.reduce((a, b) => {
    const newValue = Array.isArray(b) ? flat(b) : b
    return a.concat(newValue)
    }, [])

    console.log(flat(testArray))
  3. tobiaslins renamed this gist Mar 3, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. tobiaslins created this gist Mar 3, 2017.
    10 changes: 10 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    const testArray = [[1,2,[3]],4]

    const flat = (arr) => {
    return arr.reduce((a, b) => {
    const newValue = Array.isArray(b) ? flat(b) : b
    return a.concat(newValue)
    }, [])
    }

    console.log(flat(testArray))