Nick Bewley
Nick Bewley

Reputation: 9289

Coffeescript Array Indexing

If I have an array of photos in coffeescript

photos = [ly.p1, ly.p2, ly.p3, ly.p4, ly.p5, ly.p6, ly.p7, ly.p8, ly.p9, ly.p10, ly.p11, ly.p12]

for photo, i in photos
    photoMask = new Layer

How can I write my for loop so that the resulting photoMask objects are outputted as photoMask1, photoMask2, photoMask3 .. photoMask12 ?


EDIT: Further elaboration

Maybe the best way to explain this is what I am trying to do in psuedocode:

for photo, i in photos
    photoMask[i] = new Layer
    photoMask[i].addSubLayer(photo)

So ly.p1 would have a corresponding photoMask1. That way, I can access photoMask1 separately and independently.

Upvotes: 0

Views: 193

Answers (1)

Exinferis
Exinferis

Reputation: 687

While I agree to the commenters about this being a bit strange, you could use something like this:

photos = [ly.p1, ly.p2, ly.p3, ly.p4, ly.p5, ly.p6, ly.p7, ly.p8, ly.p9, ly.p10, ly.p11, ly.p12]

masks = {}

for photo, i in photos
    photoMask = new Layer
    masks["photoMask#{i}"] = photoMask

This will create dynamic keynames within the masks object. If you really need them globally (in the browser) you could do the same thing with the window object.

But without knowing what exactly you're trying to do, I wouldn't recommend any of the above.

Upvotes: 1

Related Questions