JavaScript Add and Remove Elements from Array

JavaScript add and remove elements from array; Through this tutorial, i am going to show you how to add, remove and replace elements from an array javaScript.

You can use the splice() method to add, remove and replace elements from an array javaScript.

JavaScript Add and Remove Elements from Array

The javascript splice() method is used to add, remove and replace elements from an array.

Syntax of javascript splice() method

array.splice(index, howmany, item1, ....., itemX)
  • The first argument specifies the location at which to begin adding or removing elements.
  • The second argument specifies the number of elements to remove.
  • The third and subsequent arguments are optional; they specify elements to be added to the array.

Here, i will take some example for add, remove and replace elements from an array javaScript using splice() method; as shown below:

1 – JavaScript remove elements from array by index

In this example, i have one array that names arr and want to remove the elements in it by index; see the following example :

 var arr = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]; 

 arr.splice(1,2);

 console.log( arr ); 

Output of the above program:

   ["one", "four", "five", "six", "seven", "eight", "nine", "ten"]
 

2: javaScript add element to array at index

To take an second example for add the elements in arr array. So let’s see the example below:

  var arr = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]; 

 arr.splice(0,0,"zero");

 console.log( arr );  

Output of the above program:

["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]

Note that:- At position 0, add the new items.

3: javaScript replace array by index

Here, i will show you with example to replace the value of an array by index using splice() method. Let’s see the below example

 var arr = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]; 

 arr.splice(5,1,6);

 console.log( arr );  

Output of the above program:

["one", "two", "three", "four", "five", 6, "seven", "eight", "nine", "ten"]

4: How to add and remove elements in array in javascript

In this example no 4, i will show you how to add or remove the item or element in array using javascript splice(). let’s see below example:

var months = ['Jan', 'March', 'April', 'June'];

months.splice(1, 0, 'Feb'); // add at index 1

console.log(months); 

months.splice(4, 1, 'May'); // replaces 1 element at index 4

console.log(months);

Output of the above program:

 // expected output: Array ['Jan', 'Feb', 'March', 'April', 'June']

 // expected output: Array ['Jan', 'Feb', 'March', 'April', 'May']

5: To remove all element from array after index in javaScript

In this example no 5, i will show you how to remove all the elements after particular index from an array. Let’s see the example below:

  var arr = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]; 

 arr.splice(3);

 console.log( arr );   

Output

 [ "one", "two", "three"];  

6: How to remove an object from an array in JavaScript

Here, i will show you how to remove Objects from Array in JavaScript. Let’s see the example below.

var arr = [
 {id:1,name:'hello'}, 
 {id:2,name:'world'},
 {id:3,name:'cool'},
 {id:4,name:'javascript'},
 {id:5,name:'jquery'}
 ];
 arr.splice(arr, 4);
 console.log (arr);

Conclusion

JavaScript add and remove elements from array; In this tutorial, you have learned how to use the JavaScript Array splice() method to remove existing elements, add new elements, and replace elements in an array.

More JavaScript Tutorials

Recommended:-JavaScript Arrays

Leave a Comment