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

What is Framer? Join the Community
Return to index
Kees Plattel
Posted May 09 - Read on Facebook

How would you construct a 2 dimensional array? I have to randomly select a 'question' and serve 4 options to answer that need to match the question that need to be displayed. Any tips would be helpful!

10 Comments

Kees Plattel

Oh quick thing; Those arrays contain text that I'm rendering using the TextLayer module. So I'll need to pull the corresponding content from there.

Nathan Krischer

A 2D array is an array of arrays. You can then access it like myArray[1][0] which would be the first index of the second nested array. [[1,2,3],[4,5,6]]

Kees Plattel

Yeah, that's how I've gotten it now;

contentarray = ['answersquestion1["answer1-1","answer1-2","answer1-3","anser1-4"]',
'answersquestion2["answer2-1","answer2-2","answer2-3","answer2-4"]',
'answersquestion3["answer3-1","answer3-2","answer3-3","answer3-4"]',
'answersquestion4["answer4-1","answer4-2","answer4-3","answer4-4"]'
]

Not sure if the formatting is right... cause I can't seem to reach the second arrays within it (they are just printed in text...)

Nathan Krischer

No your formatting is not right. It looks like you're trying to use keys. You'd need an object literal for that. See my small example at the end of my last comment. That's what a 2d array looks like. You don't name the inner arrays.

Kees Plattel

Sorry! Will give it a try, thanks a bunch already!

Nathan Krischer

If you want to use a key you could like this answers={'answer 1':[1,2,3]} then you could access like answers.answer1[1]

Kees Plattel

Nathan yes! Thanks a lot for helping out!

Sergey Voronov

hi i do it like this numberButtons=[["7","8","9"], ["4","5","6"],["1","2","3"],[".","0","="]]

Sergey Voronov
Kevyn Arnott

I'm not sure an array is the most suitable way to do this. You're using a lot of key/value concepts without the need to maintain the order. A JSON object would handle this much better. With something like questions = {q1:{text:"What does...?, answers:[a1,a2,a3,a4]}} The array of answers is nested in a larger JSON object. This makes it much easier to reference everything: questions.q1.text or questions.q1.answers.

Read the entire post on Facebook