Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript delete an object key array value

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 ?

like image 679
itsme Avatar asked Sep 06 '11 13:09

itsme


2 Answers

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/

like image 150
Jamiec Avatar answered Oct 07 '22 04:10

Jamiec


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).

like image 42
alex Avatar answered Oct 07 '22 05:10

alex