This is a read-only archive of the Framer Community on Facebook.

What is Framer? Join the Community
Return to index
Shane Brown
Posted Mar 05 - Read on Facebook

I was playing with modifying the collection grid snippet to put each cell into a 2d array, but when accessing the array back to control each cell, it only recognises the first column, and then subsequent changes all call the final cell.

eg. gridArray[[0][0]].backgroundColor = "red" works as expected

gridArray[[0][1]].backgroundColor = "red" adjusts the cell at (2,2)

Printing the array contents during creation, there is a separate layer for each cell in the array. Doing the same outside of the creation loop reveals that it is seeing the same layer multiple times in the array

I've uploaded the framer file here:

I feel like I'm making a very simple mistake, but I just can't see it!


Jordan Robert Dobson

Shouldn't it be [0][0] instead of [[0][0]]?

Selecting the second row first col would be [1][0].

It's hard to tell without seeing how you built the arrays as well.

Shane Brown

If you like, you can download the project and look at it that way, otherwise in future I'll include a pastebin link too.

What happened is that I assumed that by creating an array, and calling it within the for loop, it would automatically create the necessary array object for each pass.

eg. grid = []

for i in [0..2]
for j in [0..2]
grid[i][j] = new Layer etc..

but this gives an error saying that the record doesn't exist.

In my thought process of "Oh coffeescript can be weird", I found a way that didn't error out, but also didn't work properly.

In future, I need to initialise the array with array's first! Unless there is a more obvious way I am forgetting / missing?


grid = []

for each in [0..cols]
grid[each] = new Array

for rowIndex in [0..rows-1]
for colIndex in [0..cols-1]
grid[rowIndex][colIndex] = new Layer etc.....

Jordan Robert Dobson

That makes sense to me. :)

Jordan Robert Dobson

Looking back at my code... yeah I tend always do the myArray = [] first... and setup those things before hand.

Jordan Robert Dobson

I suppose you could do:

grid = []
for row in [0..rows]
grid[row] = []
grid[row][0] = "foo"
grid[row][1] = "bar"

Read the entire post on Facebook