remove a specific item from an array using JavaScript

 

Remove last element of array
 

Using JavaScript last element of Array can be removed by setting the array length - 1.

var arr = [1, 2, 3, 4, 5, 6];
   
arr.length = ar.length - 1; // remove last element from array
console.log( arr ); // [1, 2, 3, 4]

 

Remove specific element from array 

 

Following code will remove first element from array

var arr = [1, 2, 3, 4, 5, 6];

//splice(startFromindex)
//splice(startFromindex, deleteCount)

let startFromindex = 0;
let deleteCount = 1;
arr.splice(startFromindex, deleteCount ); 
console.log( arr );  //[2, 3, 4, 5, 6]

 

Following code will remove second element from array

var arr = [1, 2, 3, 4, 5, 6];

//splice(startFromindex)
//splice(startFromindex, deleteCount)

let startFromindex = 0;
let deleteCount = 1;
arr.splice(startFromindex, deleteCount ); 
console.log( arr );  //[1, 3, 4, 5, 6]

Tags:

Share:

Related posts