How do I remove a specific value from an array? Something like: array.remove(value); Constraints: I have to use core JavaScript. Frameworks are not allowed.
Home/remove
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 array = [2, 5, 9]; console.log(array); const index = array.indexOf(5); ifRead more
Find the
index
of the array element you want to remove usingindexOf
, and then remove that index withsplice
.The second parameter of
splice
is the number of elements to remove. Note thatsplice
modifies the array in place and returns a new array containing the elements that have been removed.For completeness, here are functions. The first function removes only a single occurrence (e.g., removing the first match of
5
from[2,5,9,1,5,8,5]
), while the second function removes all occurrences:In TypeScript, these functions can stay type-safe with a type parameter:
See less