Jump to content

removing a property from an Object


ginerjm

Recommended Posts

I have this json object that I created from php data. Works well so far - I can read it, search it and add new props to it. Basically I am treating it like an associative array.

 

Here is a visual:

 

{"key1":"value1","key2":"value2"}

 

As I said - I can add things to this just fine. Problem is trying to remove a prop, such as dropping "key2" and ending up with just one element (property) in the object. I have the following code:

 

//  ok - remove this key from the attendees list and array
var elem = document.getElementById('fld_2');
var key = elem.value;
delete atts[key];
if (atts.hasOwnProperty(key))
   alert(key+" has been deleted from atts");
else
   alert(key +" was NOT deleted from atts");
I know that I have a valid value in 'key' and I know that the property does exist before doing the delete yet when done the alert message tells me the item was NOT deleted.

 

So - how do I do this? BTW - I am currently testing this with IE10 and I have seen in my research that IE may not(?) support delete.

 

PS - I have also seen and tried "delete.atts[key]" which actually breaks the code. At least what I have above runs.

Link to comment
Share on other sites

Go to the kitchen, make yourself a sandwich, eat the sandwich, then come back and read your code very carefully.

 

if atts has the property then {
	tell me that the property was removed
} else {
	tell me that the property was not removed
}
Protip: delete has a return value for whether it deleted the value.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.