Jump to content

delete all properties at once?


jcanker

Recommended Posts

New to js here, so please be gentle:

 

Is it possible to delete all properties in one fell swoop?  Given:

var tempvariable {};
tempvariable.who = "I";
tempvariable.what = "Asked a question";
tempvariable.when = "this morning";
tempvariable.where = "at the best forum board to find answers--phpfreaks.com!";

 

I understand that to delete one property would be:

delete tempvariable.when;

 

but is there an equivalent of

delete tempvariable.* ;

 

Or do I have to loop through all the properties and delete them one by one?

 

In short I want to have a function wipe out an existing object (not technically possible since you can't delete a variable in js), so I need to clear it and reinitialize the starting properties when the user selects a different choice from a list. 

Link to comment
https://forums.phpfreaks.com/topic/227754-delete-all-properties-at-once/
Share on other sites

First off, please forgive the lack of headlessCamelCase in the previous example...I know better, I'm just getting used to it...

 

Anyway, will this work?  Is there a more efficient way?

 

for (var key in tempvariable) 
{    delete tempvariable[key]; }

to "wipe" it but not necessarily delete the namespace, you can just do any of these or equivalent

 

tempvariable = '';
tempvariable = null;
tempvariable = false;

 

none of these will delete the object per se, but it will overwrite it with a simple variable of whatever value you give it.  If you want to completely remove it, do

 

delete window.tempvariable;

 

This is assuming it's a global scoped variable/object, since all global variables/objects are actually within the window object.

 

But overall point is...notice how i'm just using the object name itself, not specifying a property (or method)

Well, I guess my noob-ness got in the way of using the right vocabulary.

 

I've figured out that I don't want to delete it, because the customer object literal is created at document.ready so that it's available to all the functions.  It gets reset inside a function, so if I completely delete it and recreate it because they chose a different customer off the list, then it will only be available to that function. 

 

I solved it by adding this at the top of the function that runs when a new customer is selected from the list:

	//first, clear out any customer/properties that exist:
for (var key in customer){delete customer[key];}
//next, wipe out the divs that may have been created with the last customer:
$('#contact, #tickets, #accounting').empty();

 

 

Thank you for answering.  I'll probably the delete ability later in this project, so you've addressed it premtively :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.