9.15: 2D Arrays in JavaScript - p5.js Tutorial
HTML-код
- Опубликовано: 7 сен 2024
- In this JavaScript Tutorial, I cover two-dimensional (2D) arrays in JavaScript. I demonstrate how to create, initialize, and use the arrays with nested for loops.
Support this channel on Patreon: / codingtrain
Send me your questions and coding challenges!: github.com/Cod...
Contact: / shiffman
Links discussed in this video:
Dan's 2D Array in Processing tutorial: www.processing...
Source Code for the Video Lessons: github.com/Cod...
p5.js: p5js.org/
Processing: processing.org
For More JavaScript Tutorials: • 9: Additional Topics -...
Help us caption & translate this video!
amara.org/v/RS64/
📄 Code of Conduct: github.com/Cod...
I like the fact you aren't afraid to show where u made mistakes or got confused. Good explanation.
directly from the P5 included Examples, you can make a 2D array very much just like as in c, as follows;
arr = []; in globals (as per usual), then just:
for (var c = 0; c < cols; c++) {
arr[c] = []; //ah ha !!!
for(var r = 0; r < rows; r++) {
arr[c][r] = random(255);
}//4r
}//4c
seems you can declare it empty with arr = [[],[]]; but you need line 2 anyway. works like a champ !
I will never stop thanking you for this knowledge.
Thank you so much dude these two dimensional arrays were giving me trouble.
You are so positive and helpful, i love your tutorials ! Please keep it up, thanks ! :)
+iPHYZLL , thank you!
i'm learning this stuff right now for a little project and really appreciate being shown the primitive things and ideas for a function
I've seen people use these weird 2 dimensional arrays before and I never knew it was a common thing, thanks for this!
thanks!
They are pretty common in a lot of areas like database (array of posts has an array of comments). If you work with JSON you will find 2D arrays all the time. When doing game development you use 2D arrays for positioning objects. Each array in the array of positions stores [x,y] sometimes [x,y,z]. Those are a few cases that I remember right now. But they are all over the place.
THANK YOU SO MUCH, I WAS SO LOST WITHOUT THIS
Thanks a lot Daniel!
this is the only workable method for multidimensional array.
It works for 3 dimensions as well.
The Coding Train lost his train of thought lol! New fav channel btw, very good teacher and nice charisma!
You the best.
thank you!
Very solid educator
I want to give a suggestion. Tho I'm 2 years late😅 But if you want to reopen a tab you closed, you could open your web browser and press Shift+Ctrl+T it should work. If it doesn't then, well I don't know.
Dan, i want to be able to fill with a specific color in the 2d array as opposed to a random one. How do i identify and fill a square with a specific colour? Thanks :-)
2022 and still saving lives✌😂
How do you target each individual box on the grid? (for example: if need to colour a specific box red)
Cols and rows are conceptually inverted, isn't it?
It's a good point. I've always done [cols][rows] b/c of being so used to (x,y) but the more I teach about matrices the more I see the convention of rows x cols. This is a recent discussion: github.com/processing/processing-docs/issues/676
that was so helpful and you made fun to watch
You can use console.table(arr);
he feels like paul gilbert(guitarist) of programming
I think i will stick with making array[width*height] and then just using array[ y * array_width + x] formula :P Seems like less of a hassle :P
Daniel can you re-visit this topic in future videos?
thanks! That helped!
I "hoped" i learn something new and realized i already using 2d arrays without knowing about this as an used method.
Thanks. Perfect explanation. I missed step array[i] = [] in my code, used array[i][k] = x instead right away and it didn't work out. It's weird you can't initialize size of array in JS
So, say a 40x40 array of 0's and 1's to decide if content is locked or not (if 1 unlock content if 0 lock content) is there a better way to do this in terms of efficiency ? Say you call the array 'journals' and check the entire array for 1's and 0's to see if the corresponding place in the timelin (canvas drawn interactive timeline that shows the journals obtained) Is unlocked
'Command + Shift + T' on MAC will open your last closed tab :)
Thank you Jeesun!
hey dan is it like [row][column]............but u were saying [column][row].....why is that
you are amazing
Thanks so much for the video!!
Nice video
thanks for watching!
Good explanation....>3
didn't get the same result, random is not a function error is what i get
Thank you!
the source code for this tutorial is not on github, can you put it again? or tell me where is it, if it actually is on github but i didn't see it, please
thnx so much solved my problem
Hello. What if Idon't know in advance what number of cols and rows I will need ?
Thank you very much. It makes me satisfy to see, that an profi say what I'm thinking the last days and hours of learning: These f*#! moredimensual arrays confuse me :)
Blocks[i] = []; i did this but it says
Uncaught TypeError: Cannot set property '0' of undefined (sketch: line 61)
Love you
AMAZINGGG!!!
thank you!
Nice! Thank you!
hey dan could u slow down a little bit a tiny bit ........because my video slow down button is not working......:-)
You Sir are pure goodness!!
What shortcut key you use for remove the comment into code ?
Command slash!
Sir can you teach me pointers * or guide me to a good like with your qualitY. I struggle with it a low
Thanks Dan, is there going to be a video on objects in 2D array?
Great idea for a follow up, feel free to suggest! github.com/CodingRainbow/Rainbow-Topics/issues/100
code line 8 ----> 10 Error detected
i = rows
j = cols
for every cols of rows
can you make a video about collision AABB and how to react to it?
Feel free to suggest: github.com/CodingRainbow/Rainbow-Topics/issues
but the real question is: can you make a 3D array?
seriously, can you make one? im interested :P
Yes, any dimension is possible, they are really just arrays of arrays.
The Coding Train So in theory you van make An 123456789D array if I'm right :P
BTW, I love your tutorials! They are really helpful, and they have got me into p5.js, which I never heard of until I saw your video's!
would you willing to write a js for me? I have been looking for someone who can do a small job for my website.
You, I mean "me", have to learn fundamental things before you start building your own stuff in a reasonable speed. 2D array is fundamental.
i love you
Python is just so much more simple Method: arr = [[random.randint(0, 1) for y in range(Rows)] for x in range(Cols)]
Sir could you make the java video tutorial series right from basics.. please please..
I am confused
Confusion means you are learning
Your whiteboard is crooked. It's hurting my brain... 🤣
hi! where i can find the code?
let make2Darr = function(r, c) { return new Array(c).fill([]).map(e => new Array(r)); }
;)
oh wow, mind blown. github.com/CodingRainbow/Rainbow-Topics/issues/100
Too sporadic.. Screens zooming all over.
First time I watched this...very confusing.
I just losing my concentration because of laughing(over ),shaking of hands and body