JavaScript- Remove particular element from an array.?

JavaScript- Remove particular element from an array.?
----------------------------------------------

Find the index of the array element you want to remove using indexOf, and then remove that index with splice.

The splice() method changes the contents of an array by removing existing elements and/or adding new elements.


  1. const dataSet = [5, 7, 8, 10];  
  2.   
  3. console.log(dataSet); // [5, 7, 8, 10]  
  4.   
  5. const index = dataSet.indexOf(10);  
  6. if (index > -1) {  
  7.   dataSet.splice(index, 1);  
  8. }  
  9. console.log(dataSet);  
  10. // This will be the output= [5,7,8]  

 

Categories: Java Script Tags: #ES6, #JavaScript,

Newsletter Subcribe

Receive updates and latest news direct from our team. Simply enter your email.