Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jquery中,如何通过索引[“key”或值删除数组元素_Javascript_Jquery - Fatal编程技术网

Javascript 在jquery中,如何通过索引[“key”或值删除数组元素

Javascript 在jquery中,如何通过索引[“key”或值删除数组元素,javascript,jquery,Javascript,Jquery,在jQuery/JavaScript中,如何删除数组元素 比如: array.remove(array["key"]); // or array.remove("value") 根据您的代码判断,听起来您想删除对象的属性,您可以使用: 关于在JavaScript中使用对象的非常有用的指南可以在上找到。数组[“key”]不是数组的键(JavaScript中没有关联数组,如果您来自PHP,它们可能看起来像它们,但它们是对象),但是对象的属性,我认为您可以使用delete delete arr

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

比如:

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

// or 

array.remove("value")

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

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

数组[“key”]
不是数组的键(JavaScript中没有关联数组,如果您来自PHP,它们可能看起来像它们,但它们是对象),但是对象的属性,我认为您可以使用delete

delete array.key

将从数组水果位置2删除1个项目,即
苹果
对于数组使用以下方法:

var array = [1, 2, 3, 4, 5];
array.splice(2, 1);
console.log(array); // [1, 2, 4, 5]
您可以使用自己的函数删除(首次出现)数组中的某个元素:

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]
如果要从对象中删除项目,请使用
删除
语法:

var a = {key1: 'val1', key2: 'val2'};
delete a.key1;
console.log(a); // {key2: 'val2'}
同样,您可以创建自己的函数来处理此问题:

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'}
更新

  • 尽管这只是一个例子,正如@Eric所指出的,修改对象的原型并不是一个好主意。因此,我重新编写了一些不会改变对象状态的示例
  • 添加了一个检查数组中是否存在元素。如果它不存在,重新返回的索引将是
    -1
    ,拼接方法将删除最后一个元素(数组末尾的第一个元素)。谢谢,@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;
    };
    
    当然,由于问题被标记为
    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); 
    
    然后像这样使用它:

    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}
    

    数组元素还是对象的属性?无论您询问的是数组还是普通对象,StackOverflow上都多次提到了这一点。实际上,我无法判断您是要删除属性、按索引删除数组项还是按值删除数组项。NARQ IMO。由于这个答案涵盖了所有可能的变体(数组/对象+键/值),我们可以美化问题,然后将每个“删除”问题链接为重复问题吗?这是一个例子。您可以根据实际结果推断参数是什么,也可以阅读到splice方法文档的postet链接。第一个参数是要从何处删除元素的索引,第二个参数是要删除的元素数。
    Object.prototype.remove=…
    -不要这样做!。如果我的对象是
    {add:'val1',remove:'val2'}
    this.splice(this.indexOf(el),1)将有问题。这将
    collection.splice($.inArray(键,集合),1)
    至少在Chrome中,如果a是一个对象,delete a.b返回true(即它不返回任何错误),即使a['b']不存在,因此似乎没有必要进行this.hasOwnProperty(el)检查。检查el是否为对象可能会很有用。虽然array[x](使用“x”中的变量)与array.xdelete不同,但会保留空值,但仍需要重新加载对象,尽管会得到删除的空索引
    (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); 
    
    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}