ginerjm Posted March 17, 2016 Share Posted March 17, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301030-removing-a-property-from-an-object/ Share on other sites More sharing options...
requinix Posted March 17, 2016 Share Posted March 17, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301030-removing-a-property-from-an-object/#findComment-1532151 Share on other sites More sharing options...
ginerjm Posted March 17, 2016 Author Share Posted March 17, 2016 doh! Quote Link to comment https://forums.phpfreaks.com/topic/301030-removing-a-property-from-an-object/#findComment-1532152 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.