javascript Javascript删除一个对象键数组值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7320779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:44:15  来源:igfitidea点击:

Javascript delete an object key array value

javascriptjqueryobject

提问by itsme

I have this object:

我有这个对象:

object = {
   key:["1","2","3","4","5"],
   key2:["5","7","8","9"]
}

How do I delete an object key and how to delete an object key value ?

如何删除对象键以及如何删除对象键值?

回答by Jamiec

For deleteing a property from an object you can use

要从对象中删除属性,您可以使用

delete object.key

For deleting an item from the array, you could use many methods, one of which is to make use of jQuery's grepmethod:

要从数组中删除项目,可以使用多种方法,其中一种方法是使用 jQuery 的grep方法:

// removes "5" from the values
object.key2 = $.grep(object.key2,function(x) { return x != "5"});

Live examples: http://jsfiddle.net/rbREb/

现场示例:http: //jsfiddle.net/rbREb/

回答by alex

How do I delete an object key and how to delete an object key value ?

如何删除对象键以及如何删除对象键值?

Use the deleteoperator to remove a property from an Object.

使用delete运算符从Object.

delete object.key

Removing the property will remove its associated value (or at least mark it for garbage collection).

删除该属性将删除其关联的值(或至少将其标记为垃圾收集)。

回答by Lesta Crenay

you can use delete object[key]. This will delete both the key and value

您可以使用删除对象[key]。这将删除键和值