Question

So I'm having trouble figuring out how to compare two arrays for differences and be able to find which elements do not exist in each other. Most examples talk about object lookup for use with a "for each in" loop. That much makes sense, but I have no idea what is going on here:

var item:Sprite;    
object_lookup[item] = true;

I'm quite confused because I've never seen anything other than an integer inside of [] such as with arrays.

Was it helpful?

Solution

Object is a dynamic, that means you can assign properties to it at runtime:

var o:Object = {};

o.sayHi = "Hi, it's me";
trace(o.sayHi); //traces: Hi, it's me

o.sayHiWithFunction = function () { return "Hi with function" };
trace(o.sayHiWithFunction()); //traces: Hi with function

If the property you want to assign is not a valid identifier, you have to use the [] and put it as a string, example:

var o:Object = {};
o.this = "Yes, it is this!"; // Error, this is a keyword! Won't compile
o["this"] = "And the correct this!"; //And this one works!
o["more words"] = "More words"; //Works too

Your code is confusing. If object_lookup is an Object instance, you created a property on it called null but you set this property to true. That means that it will have absolutely nothing to do with the Sprite item you declared above it. Which is null, of course, as you didn't assign a sprite object to it and that's why the property name gets evaluated to null. Do not confuse here: the property name "null" is just a string, it has nothing to do with the type null. Now, the for in loop loops thru all the property names of an Object, and if you know the property names, you can actually look up the values, too. Looks something like this:

var o:Object = {a:"A", b:"B", c:"C"}; // This is equivalent to o.a = "A", o.b = "B", o.c = "C"... which is aquivalent to o["a"] = "A" ;)
for(var i in o) {
    trace(i, o[i]) //traces: c C, a A, b B
}

The for each in is a bit different, if you trace i, you will see the values, not the property names as with for in loop. Otherwise do what Vesper suggested you in the comment, read about Dictionary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top