JavaScript Array Search And Remove String.
Last Updated :
16 Aug, 2024
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:
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' ]
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