JavaScript - Remove specific element from an array
Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
-
MarPlo
- Posts: 179
JavaScript - Remove specific element from an array
I have an array of integers:
Is there a simple way to remove a specific element from an array? The equivalent of something like:
I have to use pure JavaScript - no frameworks.
Admin
Use the indexOf() to find the index of the element you want to remove, then apply splice() to remove it:
Code: Select all
var array = [2, 5, 9, 8];
var index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}
console.log(array); // [2, 9, 8]
- The second parameter of splice() is the number of elements to remove.
- splice() modifies the array in place and returns a new array containing the elements that have been removed.
Note: indexOf() is not supported in IE7-8.
drakebohan
You can use the
arrayObject.filter() method :
net-informations.com/js/progs/remove.htm
Example, remove 1 specific item:
Code: Select all
var rValue = 'three'
var arrayItems = ['one', 'two', 'three', 'four', 'five', 'six']
arrayItems = arrayItems.filter(item => item !== rValue)
console.log(arrayItems)
Output:
Code: Select all
[ 'one', 'two', 'four', 'five', 'six' ]
Example, remove multiple items:
Code: Select all
let forDeletion = ['two', 'three', 'four']
var arrayItems = ['one', 'two', 'three', 'four', 'five', 'five' , 'six']
arrayItems = arrayItems.filter(item => !forDeletion.includes(item))
console.log(arrayItems)
Output:
Similar Topics
-
Hide element if data contains specific text
HTML - CSS
First post
Is there a possibility to hide HTML elements if the 'data-' attribute contains a specific piece of text?
For example: Hide the Divs that it's data...
Last post
You can do this with either CSS or Javascript.
CSS:
/* with specified elements */
div {
display: none;
}
/* or global */
{
display: none;...
-
Push an array into the same array JS
JavaScript - jQuery - Ajax
First post
I'm trying to push an array into the same array in javascript, But it doesn't seem to be working; the third element is added continuously.
Here is...
Last post
You are trying to push the same reference to the array. So, when the array updated later, the array inside the element will be updated as well.
To...
-
Remove backslash from string
PHP - MySQL
First post
How can I remove backslashes from string in php?
I tried the following code.
$str ='abc-\123';
$str = stripcslashes($str);
echo $str; //...
Last post
You can use str_ireplace() to remove backslash from string in php, but like in the following code (add two backslashes into the removing argument):...
-
Suggestion - Have SSEP remove stale urls from database after re-indexing
Scripts from this website
First post
Hello, I run the ssep_cron script daily and it works great for re-indexing the registered pages in the database. It would be great if a function...
Last post
The deletePages() method deletes the rows from 'ssep_url_', and 'ssep_pgd_' tables.
Basically, it cleans the tables; so, call that method before...
-
Check if multidimensional array
PHP - MySQL
First post
How can I check in php if an array is multidimensional or not?
I get a JSON from a third-party application and I parse it in php. But sometimes data...
Last post
You can check with two count() functions. If you add a second argument as True it will recursively count the array.
Here is an example:
$arr =[...