7

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 ?

3 Answers 3

19

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 grep method:

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

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

Sign up to request clarification or add additional context in comments.

Comments

6

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

Use the delete operator to remove a property from an Object.

delete object.key

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

3 Comments

and what if i want to delete only the property value ?
@Ispuk You could assign to it undefined or null.
@Ispuk If you want to delete only its value, you could set it to null. e.g. object.key = null.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.