How to Remove Array Element by Value in JavaScript

The best way to remove an element from an array based on the value in JavaScript is to find index number of that value in an array using indexOf() function and then delete particular index value using the splice() function. For example use following code.

Sample JavaScript Code:



Explanation:

  • Line 1 – We have initialized an array named arr with few static values.
  • Line 2 – Now we use indexOf() function to find the index number of given value in array. If given value found in array , it will return index number, else it will remove values less than 0.
  • Line 3 – First check if return index number is >=0, then only delete the value from that index from array using splice() function.

Thanks for Visit Here

Comments