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

JavascriptWeb DevelopmentObject Oriented Programming

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 ]
raja
Published on 03-Sep-2020 07:42:20
Advertisements