Randel Ramirez
Randel Ramirez

Reputation: 3761

In jquery how do I remove an array element either via index["key"] or value

In jQuery/JavaScript, How do I remove an array element?

something like:

array.remove(array["key"]);

// or 

array.remove("value")

Upvotes: 21

Views: 107837

Answers (4)

gion_13
gion_13

Reputation: 41533

For arrays use the splice method :

var array = [1, 2, 3, 4, 5];
array.splice(2, 1);
console.log(array); // [1, 2, 4, 5]

You could make your own function to remove (the first occurrence of) a certain element in the array :

Array.prototype.remove = function(el) {
    return this.splice(this.indexOf(el), 1);
}
var arr = [1, 2, 3, 4, 5];
arr.remove(4);
console.log(arr); // [1, 2, 3, 5]

If you want to remove an item from an object, use the delete syntax :

var a = {key1: 'val1', key2: 'val2'};
delete a.key1;
console.log(a); // {key2: 'val2'}

And yet again you can make your own function to handle this :

Object.prototype.remove = function(el) {
    if (this.hasOwnProperty(el)) {
        delete this[el];
    }
    return this;
}
var a = {key1 : 'val1', key2: 'val2'};
a.remove('key1');
console.log(a); // {key2: 'val2'}

Update :

  1. Although this was just an example, as @Eric pointed out, it's not a very good idea to modify an object's prototype. So I've re-written examples that do not alter the object's state
  2. Added a check if the element exists in the array. If it doesn't exist, the reeturned index would be -1 and the splice method would remove the last element (the first element from the end of the array). Thanks, @amnotiam!


function remove(collection, key) {
    // if the collections is an array
    if(collection instanceof Array) {
        if(collection.indexOf(key) != -1) {
            collection.splice(collection.indexOf(key), 1);
        }
    }
    // it's an object
    else if(collection.hasOwnProperty(key)) {
        delete collection[key];
    }
    return collection;
};

And of course, since the question was tagged jquery, we can add this function as a jquery plugin :

(function($, global, undefined) {
    $.removeElementFromCollection = function(collection,key) {
        // if the collections is an array
        if(collection instanceof Array) {
            // use jquery's `inArray` method because ie8 
            // doesn't support the `indexOf` method
            if($.inArray(key, collection) != -1) {
                collection.splice($.inArray(key, collection), 1);
            }
        }
        // it's an object
        else if(collection.hasOwnProperty(key)) {
            delete collection[key];
        }

        return collection;
    };
})(jQuery, window); 

And then use it like this :

var array = [1, 2, 3, 4, 5];
$.removeElementFromCollection(array, 2); // [1, 3, 4, 5]

var object = {1: 2, 3: 4};
$.removeElementFromCollection(object, 1); // {3: 4}

Upvotes: 28

slash197
slash197

Reputation: 9034

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 1);

will remove 1 item from array fruits, position 2, which is Apple

Upvotes: 7

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

array["key"] is not the key of an array (there are no associative arrays in javascript, if you come from PHP they might look like them, but they are objects) but the property of an object, i think you can use delete

delete array.key

Upvotes: 2

Andrew Whitaker
Andrew Whitaker

Reputation: 126042

Judging by your code, it sounds like you want to delete an object's property, which you would do with delete:

var obj = { key: "value" };

delete obj["key"];

A very useful guide on working with objects in JavaScript can be found on MDN.

Upvotes: 31

Related Questions