JavaScript Array Search And Remove String. Last Updated : 16 Aug, 2024 Comments Improve Suggest changes Like Article Like Report Arrays are widely used to store and manipulate collections of data. Sometimes we may need to search for a specific string within an array and remove it. This operation can be useful in various cases, such as filtering out unwanted items, managing user inputs, or processing data from an external source. In this article, we'll explore different approaches to searching for and removing a string from an array in JavaScript.Below are several methods to search and remove a string from an array in Javascript:Table of ContentUsing indexOf() and splice()Using filter()Using reduce()1. Using indexOf() and splice()In this approach, we will use indexOf() and splice() methods. Here the indexOf() method searches the array for the string and returns its index and then we use splice() to remove the item at that index.Example: In the below example, we are using indexOf() and splice() methods to search and remove strings from the array. JavaScript // Initial array let fruits = ["apple", "banana", "orange", "mango"]; // searching and removing const index = fruits.indexOf("banana"); if (index !== -1) { fruits.splice(index, 1); } // Output after removing string console.log(fruits); Output[ 'apple', 'orange', 'mango' ] 2. Using filter()In this approach we are using the filter() method. This method creates a new array with all elements that pass a test implemented by the provided function. This method doesn’t modify the original array but returns a new array that excludes the specified element.Example: In this example we are using filter() method to search and remove string from array in javascript. JavaScript let array = ["red", "blue", "green", "yellow"]; let filteredarray = array.filter(color => color !== "blue"); console.log(filteredarray); Output[ 'red', 'green', 'yellow' ] 3. Using reduce()In this approach we will use reduce() method to create a new array that excludes a specified string. Here we iterate over the original array and we'll add elements to the accumulator only if they don't match the string we want to remove.Example: In this example we are using reduce() method to search and remove string from array in javascript. JavaScript let states = ["Maharashtra", "Karnataka", "Tamil Nadu", "Gujarat", "Rajasthan"]; let filteredStates = states.reduce((acc, state) => { if (state !== "Gujarat") { acc.push(state); } return acc; }, []); console.log(filteredStates); Output[ 'Maharashtra', 'Karnataka', 'Tamil Nadu', 'Rajasthan' ] Comment More infoAdvertise with us Next Article JavaScript Array Search And Remove String. yuvrajghule281 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads JavaScript - Remove Text From a String Here are the different approaches to remove specific text from a string in JavaScript, starting from the most commonly used approachesUsing replace() Method - Best MethodThe replace() method is a direct and widely used method to remove specific text by replacing it with an empty string "". This meth 2 min read Remove a Character From String in JavaScript In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods t 3 min read How to Remove a Specific Item from an Array in JavaScript ? Given an Array, the task is remove specific item from an array in JavaScript. It means we have an array with N items, and remove a particular item from array.ExamplesInput: Arr = [ 10, 20, 30, 40, 50, 60 ] removeItem = 40 Output: [ 10, 20, 30, 50, 60 ]Table of ContentUsing splice() MethodUsing filte 3 min read JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po 3 min read Remove Elements From a JavaScript Array Here are the various methods to remove elements from a JavaScript ArrayRemove elements from Array1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed.javascriptlet a = ["Apple", 6 min read JavaScript - Delete Character from JS String In JavaScript, characters can be deleted from the beginning, end, or any specific position in a string. JavaScript provides several methods to perform these operations efficiently.Delete First CharacterTo remove the first character from a string, we can use methods like slice, substring, or regular 2 min read How to Remove Empty String from Array in Ruby? In this article, we will learn how to remove empty strings from an array in Ruby. We can remove empty strings from an array in Ruby using various methods. Table of Content Remove empty string from array using rejectRemove empty string from array using selectRemove empty string from array using map a 2 min read String Arrays in Java A String Array in Java is an array that stores string values. The string is nothing but an object representing a sequence of char values. Strings are immutable in Java, this means their values cannot be modified once created.When we create an array of type String in Java, it is called a String Array 5 min read How to remove given object from an Array in Java? Given an array arr of N objects, the task is to remove all the occurrences of a given object from the array in Java. Example: Input: String[] arr = { "Geeks", "for", "Geeks", "hello", "world" }, removeObj = "Geeks" Output: updated arr[] = {"for", "hello", "world"}Explanation: All the occurrences of 3 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 Like