• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Basics / How To Delete a Specific Element From an Array in JavaScript

How To Delete a Specific Element From an Array in JavaScript

How To Delete a Specific Element From an Array in JavaScript

In JavaScript, there are several ways to remove a specific element from an array. The most common methods include using the splice() method, the filter() method, and the indexOf() method.

The splice() method is used to remove elements from an array by specifying the starting index and the number of elements to remove. The syntax for the splice() method is as follows:

array.splice(index, numberOfElements)

For example, if you have an array called “fruits” and you want to remove the element “banana” from the array, you can use the following code:

let fruits = ["apple", "banana", "orange"];
let index = fruits.indexOf("banana");
fruits.splice(index, 1);
console.log(fruits); // ["apple", "orange"]

In this example, the indexOf() method is used to find the index of the element “banana” in the array. The splice() method is then used to remove the element at that index, with 1 specifying the number of elements to remove.

Another method to remove a specific element from an array is the filter() method. This method creates a new array with all elements that pass the test implemented by the provided function. Here is an example of how to use the filter() method to remove the element “banana” from the array “fruits”:

let fruits = ["apple", "banana", "orange"];
fruits = fruits.filter(fruit => fruit !== "banana");
console.log(fruits); // ["apple", "orange"]

In this example, the filter() method creates a new array with all elements that do not match the element “banana”. The original array “fruits” is then reassigned to the new array, effectively removing the element “banana”.

Lastly, you can use the indexOf method to find the index of an element in an array and use the delete operator to remove the element. The delete operator will set the element to undefined instead of removing it.

let fruits = ["apple", "banana", "orange"];
let index = fruits.indexOf("banana");
delete fruits[index];
console.log(fruits); // ["apple", undefined, "orange"]

It depends on your specific use case which method to use.

Basics, Miscellaneous

Related Snippets:

  • Create a new array from an existing one
  • Detect Caps Lock Is On
  • Get an attribute on an element
  • How to Get the Query String in JavaScript

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us