Remove elements from array in JavaScript using includes() and splice()?


The includes() check whether array has a specific element, whereas splice() is used to add/remove items. Following is the code −

Example

deleteElementsFromArray = function(elements, ...values) {
   let elementRemoved = Array.from(values);
   for (var index = 0; index < elements.length; index++){
      if (elementRemoved.includes(elements[index])){
         elements.splice(index, 1);
         index--;
      }
   }
   return elements;
}
console.log(deleteElementsFromArray([80,90,56,34,79], 90, 34,79));

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo69.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo69.js
[ 80, 56 ]

Updated on: 03-Sep-2020

269 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements