My situation:
var id_tag = [1,2,3,78,5,6,7,8,47,34,90];
I would like to delete where id_tag = 90 and to return:
var id_tag = [1,2,3,78,5,6,7,8,47,34];
How can I do that?
-
7I think the question heading should be "JS - Remove an array element by value in JavaScript"kta– kta2014年01月27日 00:47:33 +00:00Commented Jan 27, 2014 at 0:47
-
2@kta It is! You used your mental-mind-powers and the heading reformed itself. Magic.User2– User22014年05月22日 08:53:57 +00:00Commented May 22, 2014 at 8:53
-
1@User2 : I do believe in magic but in this case the Author had changed the question title after I wrote my first comment.:)kta– kta2014年05月31日 17:17:01 +00:00Commented May 31, 2014 at 17:17
-
See also: Remove item from array by value and Remove specific element from an array?Bergi– Bergi2014年08月11日 16:49:52 +00:00Commented Aug 11, 2014 at 16:49
-
find here: stackoverflow.com/a/71332975/14229690Sahil Thummar– Sahil Thummar2022年05月01日 03:44:18 +00:00Commented May 1, 2022 at 3:44
10 Answers 10
You'll want to use JavaScript's Array splice method:
var tag_story = [1,3,56,6,8,90],
id_tag = 90,
position = tag_story.indexOf(id_tag);
if ( ~position ) tag_story.splice(position, 1);
P.S. For an explanation of that cool ~ tilde shortcut, see this post:
Using a ~ tilde with indexOf to check for the existence of an item in an array.
Note: IE < 9 does not support .indexOf() on arrays. If you want to make sure your code works in IE, you should use jQuery's $.inArray():
var tag_story = [1,3,56,6,8,90],
id_tag = 90,
position = $.inArray(id_tag, tag_story);
if ( ~position ) tag_story.splice(position, 1);
If you want to support IE < 9 but don't already have jQuery on the page, there's no need to use it just for $.inArray. You can use this polyfill instead.
5 Comments
If you're going to be using this often (and on multiple arrays), extend the Array object to create an unset function.
Array.prototype.unset = function(value) {
if(this.indexOf(value) != -1) { // Make sure the value exists
this.splice(this.indexOf(value), 1);
}
}
tag_story.unset(56)
1 Comment
tag_story.splice(tag_story.indexOf(id_tag), 1);
11 Comments
id_tag is not found, it'll delete the last item in the array!! You'll have to first check if id_tag was found. See my answer.I like to use filter:
var id_tag = [1,2,3,78,5,6,7,8,47,34,90];
// delete where id_tag = 90
id_tag = id_tag.filter(function(x) {
if (x !== 90) {
return x;
}
});
As a variant
delete array[array.indexOf(item)];
If you know nothing about delete operator, DON'T use this.
3 Comments
delete keyword, you will have an undefined element in the array finally.function removeValue(arr, value) {
for(var i = 0; i < arr.length; i++) {
if(arr[i] === value) {
arr.splice(i, 1);
break;
}
}
return arr;
}
This can be called like so:
removeValue(tag_story, 90);
3 Comments
Here are some helper functions I use:
Array.contains = function (arr, key) {
for (var i = arr.length; i--;) {
if (arr[i] === key) return true;
}
return false;
};
Array.add = function (arr, key, value) {
for (var i = arr.length; i--;) {
if (arr[i] === key) return arr[key] = value;
}
this.push(key);
};
Array.remove = function (arr, key) {
for (var i = arr.length; i--;) {
if (arr[i] === key) return arr.splice(i, 1);
}
};
1 Comment
You'll want to use .indexOf() and .splice(). Something like:
tag_story.splice(tag_story.indexOf(90),1);
Comments
You can use lodash.js
_.pull(arrayName,valueToBeRemove);
In your case :- _.pull(id_tag,90);
2 Comments
var id_tag = [1,2,3,78,5,6,7,8,47,34,90];
var delete_where_id_tag = 90
id_tag =id_tag.filter((x)=> x!=delete_where_id_tag);