0

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.

11
  • 1
    Why do you want to create 12 variables when an array or object would be easier to work with? Commented Jul 6, 2015 at 23:48
  • @muistooshort its the nature of the tool that I am using. Any ideas here? Commented Jul 7, 2015 at 0:17
  • 1
    Are you sure the tool requires a bunch of variables? That doesn't make sense, what is this tool? Commented Jul 7, 2015 at 2:23
  • Hey @muistooshort thanks for the comments. I added some context above if you have any ideas that might help. I appreciate your commentary so far. Commented Jul 8, 2015 at 3:18
  • .. And the tool I'm using is a prototyping tool called Framer.js. I am trying to create layers (ex photoMask1) to correspond with their parent layer (ex. ly.p1) so that I can access photoMask1 separately and independently. Currently, the photoMask objects are created, but they aren't named so I cannot access them later. Any kind of method to do this is really the goal here. Thanks a ton for any ideas. Commented Jul 8, 2015 at 3:22

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that is great to know. I added more context to the question above. I would greatly appreciate it if you have further insight into this problem. Thanks very much for your help so far.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.