Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Delete a dynamic property of a dynamic object

Guest
Sep 30, 2009 Sep 30, 2009

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?

TOPICS
ActionScript
4.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 30, 2009 Sep 30, 2009

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

Translate
LEGEND ,
Sep 30, 2009 Sep 30, 2009

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 30, 2009 Sep 30, 2009
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines