8.3.1. Common Array Methods // splice Examples (.splice)

//The general syntax for this method is:
arrayName.splice(index, number of elements to change, item1, item2, ...);

//Inside the parentheses (), only the first argument is required.

/*The splice method modifies one or more elements anywhere in the array. 
Entries can be added, removed, or changed. This method requires 
practice.*/

/*To remove elements from an array, the splice method needs 1 or 2 
arguments.

Given only one argument, splice(index) removes every entry from index
to the end of the array.*/
let arr = ['a', 'b', 'c', 'd', 'e', 'f'];

arr.splice(2);    //Everything from index 2 and beyond is removed.
console.log(arr);

//['a', 'b']


/*With two arguments, splice(index, number of items) starts at index 
and removes the specified number of items from the array.*/
let arr = ['a', 'b', 'c', 'd', 'e', 'f'];

arr.splice(2,3);    //Start at index 2 and remove 3 total entries.
console.log(arr);

arr.splice(1,1);    //Start at index 1 and remove 1 entry.
console.log(arr);

//[ 'a', 'b', 'f' ]
//[ 'a', 'f' ]


/*To add or replace elements in an array, the splice method requires
3 or more arguments.

To add elements, set the number of elements argument to 0 and follow
this with the new items.*/

//Example: 
/*splice(index, 0, new item) starts at index and INSERTS the new items.
Existing elements get shifted further down the array.*/
let arr = ['a', 'b', 'c', 'd', 'e', 'f'];

arr.splice(2,0,'hello');     //Start at index 2, remove 0 entries, and add 'hello'.
console.log(arr);

//[ 'a', 'b', 'hello', 'c', 'd', 'e', 'f' ]


/*To replace elements in an array, the number of elements argument 
must be a positive integer. Follow this with the new items for the 
array.*/

//Example:
/*splice(index, number of items, new items) starts at index and 
REPLACES the number of items with the new ones.*/
let arr = ['a', 'b', 'c', 'd', 'e', 'f'];

arr.splice(2,3,'hello', 9);    //Start at index 2, replace 3 entries with 'hello' and 9.
console.log(arr);

//[ 'a', 'b', 'hello', 9, 'f' ]

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source