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.

const dataSet = [5, 7, 8, 10];

console.log(dataSet); // [5, 7, 8, 10]

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

 

Leave a reply

your email address will not be published. required fields are marked *

Name *
Email *
Website