Friday, February 12, 2010

Delete operator in JavaScript

I never knew that a keyword named as Delete existed in javascript.
And that was until I started studying the Lotus Domino Designer XPage
Reference documents. This seems to be a pretty useful operator and is used to
deletes an object, an object's property, or an element at a specified index
in an array.

The following code fragment will help you with better understanding of the same
/**************************************************************/
var arr = {
    "number": 42,
    "year" : 2007,
    "hello" : "world",
     "foo" : "bar"
 }

for(var ele in arr) {

    alert(ele + " : " + arr[ele]);
}
 

delete arr['hello']; //Removes the 'hello' key of the array

for(var ele in arr) {
    alert(ele + " : " + arr[ele]); //The 'hello' key and its value will be missing
}

/**************************************************************/

No comments:

Post a Comment