Skip to content

Instantly share code, notes, and snippets.

@ricardoreis
Last active May 23, 2020 18:45
Show Gist options
  • Select an option

  • Save ricardoreis/4b63c102e1e0fc81e882ab065e23ff27 to your computer and use it in GitHub Desktop.

Select an option

Save ricardoreis/4b63c102e1e0fc81e882ab065e23ff27 to your computer and use it in GitHub Desktop.

Revisions

  1. ricardoreis revised this gist May 23, 2020. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions splice.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    /* Sybtax:
    * Array.splice(position, 0, new_element_1, new_element_2, ...)
    /* Syntax:
    * Deleting elemts
    * Array.splice(position, num);
    *
    * Inserting elements
    * Array.splice(position, 0, new_element_1, new_element_2, ...);
    */

    let colors = ['red','green','blue'];
  2. ricardoreis created this gist May 23, 2020.
    20 changes: 20 additions & 0 deletions splice.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    /* Sybtax:
    * Array.splice(position, 0, new_element_1, new_element_2, ...)
    */

    let colors = ['red','green','blue'];

    colors.splice(2, 0, 'purble');
    console.log(colors); // [ 'red', 'green', 'purble', 'blue' ]

    colors.splice(1, 0, 'yellow', 'pink');
    console.log(colors); // [ 'red', 'yellow', 'pink', 'green', 'purble', 'blue' ]


    let languages = ['C', 'C++', 'Java', 'JavaScript'];

    languages.splice(1, 1, 'Python');
    console.log(languages); // [ 'C', 'Python', 'Java', 'JavaScript' ]

    languages.splice(2, 1, 'C#', 'Swift', 'Go');
    console.log(languages); //[ 'C', 'Python', 'C#', 'Swift', 'Go', 'JavaScript' ]