Well, there are different approaches to do this. Do you have an idea how to do this? Don’t worry; we have explained those approaches in the below section.
So without discussing more, let’s start with the details.
How can I remove a specific item from an array?
As already mentioned there are different approaches to do this. We have explained the three most used methods. And these are:
- .splice( )
- .indexOf( ) and .splice( )
- Traditional method (using the For Loop)
Let’s find out how these methods work with useful examples.
1) .splice method
[This will be used if you know the index of the item you want to remove.]
The .splice() method is one of the built-in methods in JS for the array objects. Using it, you can change the content of the array by removing or replacing the items with new ones.
With the help of the .splice() method, you can easily modify the original array. If you want to get the removed items like a new array, you can do that using .splice().
Let’s understand how to use .splice()
[ Note: You might have heard that an array in JS can store any number, string, boolean, or null. Apart from this, the array can store the combination of these data types. That is why I have taken the example of string data type.]
Syntax: Array.splice(start, removeCount, Item1, Item2, …, ItemN)
Suppose you have two month’s names and two week day’s names as:
January, February, Monday, Tuesday
And you want to separate the week day’s names by removing the month’s names. Then, write the code as:
In this code, the splice starts removing all the items from the 2nd index value. And it provided the output of the current items stored in an array.
[NOTE: The indexing in JS starts from 0 and count as 1,2,…n]
Now the question is “how can I remove a specific item from an array” using the .splice() method. Let’s understand it with a shortcode:
You can see that we have used .splice(1,2). This means we want to remove 2 items that will start from 1 index value.
After this, we have got the output of the current array as names. And removed items of the array as removed.
This is how to remove a specific item in an array using the .splice() method. (Only if you have the index value of the item you want to remove).
2) indexOf () and .splice() method
[This will be used if you do not know the index of the item you want to remove.]
Sometimes, the user doesn’t know the exact index of the item. In that case, he/she can use the .indexOf() along with the .splice() method.
The indexOf() will always return the index of the item. But if the item is not present in the array, then it will return -1. Let’s take an example of it first:
As you can see “Wednesday” is not in the names1 array; therefore, the output is -1. On the other hand, “Monday” is present in the names2 array at the 3 positions with the 2nd index value, the output is 2.
Now, how to remove a specific item in an array using the .indexOf() and .splice() method.
Let’s take an example of it:
In the above code, the first indexOf checks the index of the item “February” of the names array. That is stored in the days. Then, the splice takes the index value from the days and removes 2 items from that index. Further, the result will be stored in removed.
This is how can I remove a specific item from an array without even knowing the index of the item.
Can I remove two or more similar items from an array using the .splice() method?
No, you can’t!!
It is not possible to remove two or more similar items from an array. Let’s take an example of it:
let names = [“January”, “February”, “Monday”, “February”, “Tuesday”];let removed = names.splice(1, 1);console.log(removed);console.log(names);
[“February”][“January”, “Monday”, “February”, “Tuesday”]
Here, February is at index values 1 and 4, but it has removed the 1 index valued item. Then it terminates the processing (once it gets the 1st indexed item). So, the .splice() method is not suitable for removing two or more similar items.
3) Traditional method (using the For Loop)
This is an alternate, but effective method that is used for removing the item from an array. So, how can I remove a specific item from an array in JS using a for loop method?
Let’s take an example of it for a better understanding:
Here, we have defined the colors variable array first. Then, use a for loop, where variable j has the same length as that of the colors array that starts from index 0 to the final length item.
Now, the if statement will execute when variable j gets the item red. After this, .splice() will remove the j variable that is red and keep removing items till 2 elements get removed. Finally, we get the current elements of the colors array as [“green”, “black”].
You can see that the colors array has two elements. This is how can I remove a specific item from an array using a for a loop.
Use and practice the above-mentioned methods to understand the functions’ concepts effectively. I think you will definitely run your programs without facing any issues. But in case you have any issues; let me know about them.
Let’s test what you have studied and understood from this blog!
(A) i, iii & iv only
(B) i, & iv only
(C) ii, & iii only
(D) All i, ii, iii & iv
|Correct Answer: (D) |
splice – Delete a specific element from an array with the particular index filter – Enables the user to delete elements from the Array programmatically shift – Delete the element from the Array’s beginning pop – Delete the element from the Array’s End
- What will be the output of the below code?
(A) [“Sat”, “Sun”]
(C) [“Mon”, “Tue”, “Wed”, “Thurs”, “Fri”]
(D) None of these
|Correct Answer: (C)|
Splice has removed the 2 elements starting from the 2nd index and added “Wed” at the 2nd index.
- What will be the output of the following code?
(B) [“Mon”, “Tues”]
(D) None of these
|Correct Answer: (B)|
Splice has removed all the elements from the 2nd Index.
- What will be the output of the following program?
var j = [“Alice”, “Tom”, “Alice”];
(C) 0, 2
(D) None of these
|Correct Answer: (A)|
As you can see that at 0 (zero) indexes the “Alice” name is found, it will show its index and terminate without executing a similar item further.
|Correct Answer: (B)|
Splice can only remove a single similar item from the given array.