Skip to content

Instantly share code, notes, and snippets.

@ricardoreis
Last active May 23, 2020 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricardoreis/4b63c102e1e0fc81e882ab065e23ff27 to your computer and use it in GitHub Desktop.
Save ricardoreis/4b63c102e1e0fc81e882ab065e23ff27 to your computer and use it in GitHub Desktop.
/* Syntax:
* Deleting elemts
* Array.splice(position, num);
*
* Inserting elements
* 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' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment