Home >Web Front-end >JS Tutorial >How Can I Remove a Specific Element from a JavaScript Array Using Only Core JavaScript?
Core JavaScript: Removing Specific Items from Arrays
Question: Can I remove a particular element from an array? I would like to use a method similar to array.remove(value).
Note: Core JavaScript is required; frameworks are not permitted.
Answer:
To remove a specific value from an array in JavaScript, you can use the following steps:
Explanation of splice:
The splice method modifies an array by:
Example Code:
const array = [2, 5, 9]; console.log(array); // Search for specific element const index = array.indexOf(5); // Remove element if found if (index > -1) { // only splice array when item is found array.splice(index, 1); // 2nd parameter means remove one item only } // Resulting array with removed element console.log(array);
Output:
[2, 5, 9] [2, 9]
The above is the detailed content of How Can I Remove a Specific Element from a JavaScript Array Using Only Core JavaScript?. For more information, please follow other related articles on the PHP Chinese website!