Skip to content

Instantly share code, notes, and snippets.

@tsertkov
Last active October 22, 2019 09:30
Show Gist options
  • Select an option

  • Save tsertkov/9ba84b56ac425a708b5159e5bba76aeb to your computer and use it in GitHub Desktop.

Select an option

Save tsertkov/9ba84b56ac425a708b5159e5bba76aeb to your computer and use it in GitHub Desktop.

Revisions

  1. tsertkov renamed this gist Oct 22, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. tsertkov revised this gist Oct 22, 2019. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -18,4 +18,9 @@ const a1 = [0, 2, 4, 6, 8, 8]
    const a2 = [1, 2, 3, 4, 5, 6]
    […new Set(a1)].filter(item => a2.includes(item))

    // Unique elements

    const a = [0, 1, 2, 1, 1, 0]
    Array.from(new Set(a))

    ```
  3. tsertkov revised this gist Oct 22, 2019. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -13,9 +13,9 @@ const a = ['blue', 'yellow']
    a[(Math.floor(Math.random() * (a.length)))]

    // Arrays intersection
    var numOne = [0, 2, 4, 6, 8, 8];
    var numTwo = [1, 2, 3, 4, 5, 6];
    var duplicatedValues = [new Set(numOne)].filter(item => numTwo.includes(item));
    console.log(duplicatedValues); // returns [2, 4, 6]

    const a1 = [0, 2, 4, 6, 8, 8]
    const a2 = [1, 2, 3, 4, 5, 6]
    […new Set(a1)].filter(item => a2.includes(item))

    ```
  4. tsertkov revised this gist Oct 22, 2019. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -12,4 +12,10 @@ Array.from([{ 'key': 1, 'value': 'val1'}, { 'key': 2, 'value': 'val2' }], ({valu
    const a = ['blue', 'yellow']
    a[(Math.floor(Math.random() * (a.length)))]

    // Arrays intersection
    var numOne = [0, 2, 4, 6, 8, 8];
    var numTwo = [1, 2, 3, 4, 5, 6];
    var duplicatedValues = […new Set(numOne)].filter(item => numTwo.includes(item));
    console.log(duplicatedValues); // returns [2, 4, 6]

    ```
  5. tsertkov revised this gist Oct 22, 2019. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,9 @@
    Array.from([{ 'key': 1, 'value': 'val1'}, { 'key': 2, 'value': 'val2' }], ({key}) => key)
    Array.from([{ 'key': 1, 'value': 'val1'}, { 'key': 2, 'value': 'val2' }], ({value}) => value)

    // Get random value from array

    const a = ['blue', 'yellow']
    a[(Math.floor(Math.random() * (a.length)))]

    ```
  6. tsertkov created this gist Oct 22, 2019.
    10 changes: 10 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    # JavaScript array tips & tricks

    ```javascript

    // Array mapping with .from()

    Array.from([{ 'key': 1, 'value': 'val1'}, { 'key': 2, 'value': 'val2' }], ({key}) => key)
    Array.from([{ 'key': 1, 'value': 'val1'}, { 'key': 2, 'value': 'val2' }], ({value}) => value)

    ```