
Copy link to clipboard
Copied
I am using a regular Flash object as a simple data container, and I want to remove a particular property of that object. Then that object gets passed on to a for..in loop. But even after using delete to remove the property, the key still shows up in the loop.
For example:
var obj:Object = { a: "a", b: "b", c: "c" }
delete obj['b'];
for ( var prop:String in obj ) {
trace( prop + ": " + obj[ prop ] );
}
// a: a
// b:
// c: c
So deleting the property seems to be the same as assigning its value to null.
Is there any way to immediately remove the property from the object altogether so that it is no longer looped over in a for..in loop?
It is not sufficient to check the property for a null value since a property with a null value is not the same as not having the property in the first place, and represents a different state. I don't want to set the property to null, I want to remove the property.
Is this possible in AS3?
1 Correct answer
That isn't how your code works on my machine.
var obj:Object = { a: "a", b: "b", c: "c" }
trace("Before");
for ( var prop:String in obj ) {
trace( " "+prop + ": " + obj[ prop ] );
}
trace("\nAfter");
delete obj['b'];
for ( prop in obj ) {
trace( " "+prop + ": " + obj[ prop ] );
}
On my machine here is how it works out:
Before
b: b
c: c
a: a
After
c: c
a: a
Copy link to clipboard
Copied
That isn't how your code works on my machine.
var obj:Object = { a: "a", b: "b", c: "c" }
trace("Before");
for ( var prop:String in obj ) {
trace( " "+prop + ": " + obj[ prop ] );
}
trace("\nAfter");
delete obj['b'];
for ( prop in obj ) {
trace( " "+prop + ": " + obj[ prop ] );
}
On my machine here is how it works out:
Before
b: b
c: c
a: a
After
c: c
a: a

Copy link to clipboard
Copied
Sorry, my mistake. My problem was a bit more complex than that and I was getting thrown by something else entirely. Thanks!

