JavaScript - Delete First Occurence of Given Element from a JS Array Last Updated : 17 Nov, 2024 Comments Improve Suggest changes Like Article Like Report These are the following ways to delete elements from a specified Index of JavaScript arrays:1. Using indexOf() and splice() - Most UsedThe indexOf() method finds the first occurrence of the element in the array, and splice() removes it at that index. This approach directly modifies the original array. JavaScript let a = [1, 2, 3, 4, 3, 5]; // Element to be deleted let x = 3; let i = a.indexOf(x); if (i !== -1) { a.splice(i, 1); } console.log(a); Output[ 1, 2, 4, 3, 5 ] 2. Using filter() methodThe filter() method is used to create a new array that excludes the first occurrence of the element, without modifying the original array. JavaScript let a1 = [1, 2, 3, 4, 3, 5]; let x = 3; let found = false; let a2 = a1.filter(value => { if (value === x && !found) { found = true; return false; // Skip first match } return true; // Include all other elements }); console.log(a2); Output[ 1, 2, 4, 3, 5 ] 3. Using findIndex() with splice()The findIndex() method returns the first occurrence of the element matching the condition, if it exists the splice() method is used to remove the element. JavaScript let a = [1, 2, 3, 4, 3, 5]; let x = 3; let i = a.findIndex(value => value === x); if (i !== -1) { a.splice(i, 1); } console.log(a); Output[ 1, 2, 4, 3, 5 ] 4. Using for loopThe for loop manually iterates over the elements of the array to find and delete the first occurrence. The loop is stopped immediately after deleting the element. JavaScript let a = [1, 2, 3, 4, 3, 5]; let x = 3; for (let i = 0; i < a.length; i++) { if (a[i] === x) { a.splice(i, 1); break; } } console.log(a); Output[ 1, 2, 4, 3, 5 ] Comment More infoAdvertise with us Next Article JavaScript - Delete First Occurence of Given Element from a JS Array A amit_singh27 Follow Improve Article Tags : JavaScript Web Technologies javascript-array Similar Reads JavaScript - Delete Elements from the End of a JS Array These are the following ways to delete elements from the end of JavaScript arrays:1. Using pop() methodThe pop() method is used to remove the last element from an array and returns the modified array.JavaScriptlet a = [1, 2, 3, 4]; // Remove last element a.pop(); console.log(a);Output[ 1, 2, 3 ] 2. 2 min read JavaScript - Delete Elements from an Index in JS Array These are the following ways to delete elements from a specified Index of JavaScript arrays:1. Using splice() MethodThe splice() method is used to remove elements directly from a specified index in an array by specifying the index and the number of elements to delete.JavaScriptlet a = [1, 2, 3, 4, 5 1 min read JavaScript - How to Get First N Elements from an Array? There are different ways to get the first N elements from array in JavaScript.Examples:Input:arr = [1, 2, 3, 4, 5, 6], n = 3 Output: [1, 2, 3] Input:arr = [6, 1, 4, 9, 3, 5, 7], n = 4Output: [6, 1, 4, 9]1. Using slice() MethodThe slice() method is used to extract a part of an array and returns a new 4 min read JavaScript- Delete from a Given Position of JS Array These are the following ways to delete an item from the given array at given position: 1. Using the splice() Method(Most efficient for in-place modifications)The splice() method is the most versatile way to remove elements from any position in an array. It allows you to specify the index where the r 2 min read JavaScript - Delete last Occurrence from JS Array These are the following ways to remove the last Item from the given array: 1. Using pop() Method (Simple and Easiest Method for Any Array) The pop() method is used to get the last element of the given array it can also be used to remove the last element from the given array. JavaScriptlet a = [34, 2 2 min read How to Remove duplicate elements from array in JavaScript ? In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array. These are the following ways: Table of Content Using filter() MethodUsing set() MethodUsing forEach() MethodUsing reduce() MethodUsing index 4 min read How to Remove First and Last Element from Array using JavaScript? Removing the first and last elements from an array is a common operation. This can be useful in various scenarios, such as data processing, filtering, or manipulation. Example:Input: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]Output: [ 2, 3, 4, 5, 6, 7, 8, 9 ]Removing first and last element in array can be do 2 min read How to delete an element from an array using JavaScript ? The array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index. In this article, we will discuss different ways to remove elements from the array. 5 min read JavaScript - Delete Element from the Beginning of JS Array These are the following ways to delete elements from JavaScript arrays:1. Using shift() method - Mostly UsedThe shift() method removes the first element from the array and adjusts its length.JavaScriptconst a = [1, 2, 3, 4]; // Remove the first element a.shift(); console.log(a);Output[ 2, 3, 4 ] 2. 2 min read Remove Duplicate Elements from JavaScript Array To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r 3 min read Like