在jquery中如何通过索引["key"]或值删除数组元素

Ran*_*rez 19 javascript jquery

jQuery/JavaScript中,如何删除数组元素?

就像是:

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

// or 

array.remove("value")
Run Code Online (Sandbox Code Playgroud)

gio*_*_13 24

对于数组,使用splice方法:

var array = [1, 2, 3, 4, 5];
array.splice(2, 1);
console.log(array); // [1, 2, 4, 5]
Run Code Online (Sandbox Code Playgroud)

您可以创建自己的函数来删除(第一次出现)数组中的某个元素:

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]
Run Code Online (Sandbox Code Playgroud)

如果要从对象中删除项,请使用以下delete语法:

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

而且你可以再做一个自己的功能来处理这个:

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'}
Run Code Online (Sandbox Code Playgroud)

更新:

  1. 虽然这只是一个例子,正如@Eric指出的那样,修改对象的原型并不是一个好主意.所以我重写了不改变对象状态的例子
  2. 添加了检查元素是否存在于数组中的情况.如果它不存在,那么reeturned索引将是-1,并且splice方法将删除最后一个元素(数组末尾的第一个元素).谢谢,@ 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;
};
Run Code Online (Sandbox Code Playgroud)

当然,既然标记了问题jquery,我们可以将此函数添加为jquery插件:

(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); 
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

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}
Run Code Online (Sandbox Code Playgroud)

  • 由于这个答案涵盖了所有可能的变体(数组/对象+键/值),我们可以美化问题,然后将每个"删除"问题链接到这里作为重复吗? (3认同)
  • `Object.prototype.remove = ...` - **不要那样做!**.如果我的对象是`{add:'val1',remove:'val2'}`,该怎么办? (3认同)
  • `this.splice(this.indexOf(el),1);`如果找不到该项,将会出现问题.这个`collection.splice($.inArray(key,collection),1);` (3认同)

And*_*ker 23

根据您的代码判断,听起来您想要删除对象的属性,您可以使用以下内容delete:

var obj = { key: "value" };

delete obj["key"];
Run Code Online (Sandbox Code Playgroud)

可以在MDN上找到有关在JavaScript中使用对象的非常有用的指南.


sla*_*197 6

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 1);
Run Code Online (Sandbox Code Playgroud)

将从阵列水果中移除1个项目,位置2,即 Apple