Тёмный
No video :(

7.5: Removing Objects from Arrays - p5.js Tutorial 

The Coding Train
Подписаться 1,7 млн
Просмотров 100 тыс.
50% 1

Опубликовано:

 

5 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 117   
@0xAndy
@0xAndy 6 лет назад
Your forEach() anxiety is forever amusing.
@SebastianScholle
@SebastianScholle 6 лет назад
Your style is unique! I think you failed to mention the REASON for using the 'splice' function. The issue with the 'delete' operator in JS is that it only removes an element of an array, however the array keeps its length resulting in an 'empty' element at the place that was deleted. Complicating the iteration of the array going forward. This is why we should all use 'splice'. Don't use 'delete'.
@xerxius5446
@xerxius5446 4 года назад
I really love how using classes makes everything so organized and "brain-friendly"
@treestrax1072
@treestrax1072 3 года назад
I've realised these videos are great visual explanations of Javascript, where you can apply the same knowledge/techniques to applications that are less about drawing pretty things (bubbles).
@graxxarecords3456
@graxxarecords3456 2 года назад
Hell yeah, this guy is such a a genius! He can explain complex concepts, that we can apply in work oriented fashion!
@MW2hairbeRt
@MW2hairbeRt 6 лет назад
Splice is a really slow function in general because it uses alot of shifting things and eventually doing some allocations (removing something that's not the beginning or end in general isn't fast usually). If you don't care about the order of your array (for example the bubbles don't need to be ordered by size or whatever), removing an element quickly is done by just swapping with the last element and then popping. Old trick but really useful to know.
@peoplethesedaysberetarded
@peoplethesedaysberetarded 6 лет назад
Another good video, Daniel, with lots of interesting AH-HA! moments. (Backwards array traversal to safeguard against skipping elements is a really solid contribution when talking about arrays.)
@DanielMuvdi
@DanielMuvdi 6 лет назад
Var learningJavaScript = Fail { if true return Cry ; i = fail; fail = true;}
@ddv2nine722
@ddv2nine722 6 лет назад
function Fail (trying) { if(trying === not-enough && brains === explotion) { return "mision Failed"} } var learningJavaScript = fail(not-enough);
@blackteeneur
@blackteeneur 6 лет назад
Shiffman? No... SHIFTMAN!
@kenhaley4
@kenhaley4 6 лет назад
Dan, before you fall in love with forEach, check out the new ES6 for..of statement. It works with object properties, as well as array elements, where as forEach is only for arrays. And with objects, it's better than for..in (if you've seen that), because for..in gives you inherited properties, which you're usually not interested in (see hasOwnProperty() method). Also for..in requires you to pass in a callback function, but for..of does not. Unfortunately, none of these newer techniques support deleting elements without skipping. In that case, afaik, you need the original for statement to iterate backwards or decrement the index.
@TheCodingTrain
@TheCodingTrain 6 лет назад
thanks for the tip!!!
@pepefubias7654
@pepefubias7654 4 года назад
jajajaja between the foreach and the slice dislexia with splice we are gonna go nuts jajaja love it what a great and funny teacher at the same keep it up!!
@RameenFallschirmjager
@RameenFallschirmjager 5 лет назад
thanks shiffman, in this video you mentioned a very important thing when differentiated between j5 specific functions and built-in vanilla javascript ones. I just wonder why you didn't do it sooner!
@Spongman
@Spongman 6 лет назад
if you're not bothered about the order of the items in the array, and you're looking through the array to remove some, then it may be more efficient to just pop() the last element off the list and place it at the position of the item you wish to remove. f you loop backwards through the list, then you don't need to adjust the index after the removal. Array.prototype.removeAt = function (i) { var other = this.pop(); if (i < this.length) this[i] = other; return other; };
@VinhoSantos
@VinhoSantos 4 года назад
Glad I'm not the only one mixing up splice and slice. It's a ridiculous name
@FredoCorleone
@FredoCorleone 6 лет назад
I'm wondering why forEach is so fearsome xD
@MisterMajister
@MisterMajister 6 лет назад
I haven't thought about that an "i--" approach to a for-loop will get rid of the splicing problem! I'd guess it's also faster since you only check the length of the array once and set a variable to that length, instead of checking it for every iteration.
@ddv2nine722
@ddv2nine722 6 лет назад
Here is another way to change the color when the mouse is above the bubbles. You dont need a function o behavior for that, lets add another caracteristic of being a bubble like " this.mouseUpon = false", and instead of return "true " when the condition of the Contain behavior is true, lets make "this.mouseUpon = true" and stay false if its not. Then in the Show behavior, we can write If( this.mouseUpon) { //change the color } else { //dont do anything }. Now that forces us, to change the condition of the If statement inside the for loop in the mousePressed function. We now should write if(bubbles[i].mouseUpon){ bubbles.splice(i, 1); }
@bunzguy6448
@bunzguy6448 2 года назад
LOL cracks me up every time he says slice ...... "splice"
@nikensss
@nikensss 6 лет назад
I find your videos really interesting and instructing. Your teaching skill, your ability to forward knowledge, is admirable. But I gave thumbs up to the video because of 1.27 :) Keep making videos this funny.
@labhamjain3915
@labhamjain3915 Год назад
I think you should have some sort of IDs assigned to bubble and have keep count of those IDs in separate array then if there is a rollover then return ID and do all the deletion stuff
@dariusseals8803
@dariusseals8803 2 года назад
Love the energy.
@TheLoGgIDK
@TheLoGgIDK 2 года назад
Shiffman keeps impressing me..
@geraldhunt8263
@geraldhunt8263 6 лет назад
Hey Dan, first off I love your videos they inspire me to continue coding everyday. I'm trying to make a 2d physics simulation in P5 where 2 circular objects (particles/bubbles) knock into each other and respond in a realistic way.I was wondering if you could either make a video about the subject or refer me to one of your older videos. Thanks in advance
@zungnguyen5300
@zungnguyen5300 5 лет назад
those p5.js are amazing dude
@marcusantenor793
@marcusantenor793 2 года назад
Hey, Great content as always, love the course. Nonetheless, I feel that this video needs to be updated on the playlist. Too many instant changes and confusions. I got lost for a while.
@jeremyyang3048
@jeremyyang3048 3 года назад
HELP !looks like this sytax :for(let i=bubbles.length-1;i>=0;i--) cannot avoid miss checking while the bubbles are overlapping. I still can delete both.
@TheChodex
@TheChodex 6 лет назад
i almost fell out of my chair at 4:46 hahahaha you are a legend :DD
@jaidanbalea2629
@jaidanbalea2629 6 лет назад
* SPLICEEEEEEEEEEEEEEEEEE
@sauhardbhandari8435
@sauhardbhandari8435 5 лет назад
5:46 I wanna use this ( * S L I C E * ) hmm yea i wannt to .. no doubt xD
@jerryrobinson958
@jerryrobinson958 4 года назад
Jaidan Balea lol
@alphapicturesentertainment
@alphapicturesentertainment 5 лет назад
for of loop: defend me at all cost! for each: *exists* for of loop: RUN
@FritsvanDoorn
@FritsvanDoorn 6 лет назад
Hi Dan, The MousePressed function is an interrupt routine and therefore it can execute during the draw function. When the for loop is executing in the draw function and the MousePressed function removes an element from the bubbles array, there will be a problem, although I think that JavaScript can handle it, but Java and other programming languages will most likely crash. Before removing an element from the array the routine must be sure that no one else is using the array. In fact, when somebody can click that fast, the MousePressed function can execute during execution of the MousePressed function and then it is possible that one of them will remove the wrong bubble. Tell me, am I wrong? You make awesome video's. And I always watch them, but not the ones that take 3 hours or so. Keep up the fun !! Thnx.
@manguitodelicioso
@manguitodelicioso 2 года назад
Genius! Great explanation.
@hory-portier
@hory-portier 6 лет назад
Yeah, you actually should use forEach here, or at least that's is what it was made for. Its quite handy in a lot of programming languages to know it but in this specific case it looks just a little bit more complicated than what you did here.
@oshgnacknak72
@oshgnacknak72 6 лет назад
ForEach: bubbles.forEach(bubble => { bubble.doStuff(); }); Make a Video about arrowfuntions
@TheCodingTrain
@TheCodingTrain 6 лет назад
I am going to!
@polojuninho
@polojuninho 6 лет назад
Awesome 😃😃😃
@coopster81
@coopster81 Год назад
Which video were you referring to with objects storing their own path?
@angelcaru
@angelcaru 4 года назад
3:35 function ellipse(){}
@__chaudharysaab__
@__chaudharysaab__ 3 года назад
Totally relating the the forEach() anxiety level
@ellsonmds5310
@ellsonmds5310 4 года назад
Wouldn't be great to avoid one function call and just check if(bubbles.length>10) bubbles.length=10 ?
@QuinnWaters
@QuinnWaters 6 лет назад
YOU>ARE>THE>BEST!!!!!
@WaqarAhmed-yy3nx
@WaqarAhmed-yy3nx 2 года назад
how can we use "splice" with "for of" loop? for ( let unicorn of bubbles) { if ( unicorn.contains ( mouseX, mouseY)){ unicorn.splice( i, 1 ) } This function doesn't work.
@albertocalabrese2958
@albertocalabrese2958 6 лет назад
I find foreach so useful, why people is against it? I'm a newbie in javascript xD
@TheCodingTrain
@TheCodingTrain 6 лет назад
I'm just being silly, it is useful!!
@soniablanche5672
@soniablanche5672 4 года назад
Keep in mind that forEach does not have access to "break" and "return" doesn't not exit the forEach loop. "return" will have the same effect as "continue" in a conventional loop, it will return from the current callback function then continue with the next one. You cannot shortcircuit a forEach unless you throw an error, but that would be using the try catch mechanism outside its intended use.
@som.shekhar
@som.shekhar 4 года назад
Important Note: If you want to delete something from an array while iterating over it, just iterate in reverse fashion, using i - - should be avoided , because you will be accessing out of the array which works fine JS as it will just return undefined, but with other languages like python accessing out of array, would throw index out of range error.
@husamk3207
@husamk3207 6 лет назад
i'm very beginner to p5.js i just want to know how to detect the collection between circles or between the sides of window, please reply and thank you so much .
@nietschecrossout550
@nietschecrossout550 6 лет назад
Could you add a tendency in the move function to move less to the e.g. top when its near to the top?
@TheCodingTrain
@TheCodingTrain 6 лет назад
yes, you could adjust the random movement to pick negative numbers more often!
@shahabsafdar2241
@shahabsafdar2241 4 года назад
Hi, I've a question, I've made a class FreeHandLineTool, I've only one object of that class, and display method of that class draws line(on mouse press) for me on canvas. I keep calling display method inside the draw method with its object(only 1). and it keep drawing lines on mouse press. I want to do undo, remove lines one by one. please help me out. Thanks!
@Violet-tb8xo
@Violet-tb8xo 5 лет назад
editor.p5js.org/ibeaducko/present/cFJNW-zz7 INSTRUCTIONS: double click to spawn a ball and click to remove a ball
@1morelight
@1morelight 3 года назад
Can you explain how the events fit inside the flow of p5.js? Mainly how they interact with the draw() loop
@WolfRose11
@WolfRose11 2 года назад
JavaScript is an Asynchronous language by default. So draw and other events could be simultaneous.
@Swapkadu
@Swapkadu 5 лет назад
Hey, I just want to access all data in nested array of objects using javascript not jquery and to display that data in to tree format in HTML DOM. How can i do that..PLEASE HELP!
@TalesCembraneliDantas
@TalesCembraneliDantas 3 года назад
If I'm in a function inside of the object, can I remove this object without knowing the index he is?
@erispe
@erispe 3 года назад
Why are your for-loops not breaking when removing elements from the list as you are iterating over it? I'm used to instead creating a new list of items that I need to delete from the array after the loop is finished. Is this just a luxury feature of JavaScript?
@zero11010
@zero11010 2 года назад
At each iteration it evaluates the length of the array again with array.length.
@baileighdabdoub8604
@baileighdabdoub8604 4 года назад
Hi! I’m trying to use splice to remove the circles like he did when you click them, BUT it keeps giving me the error that ‘bubbles[x].contains’ is not a function. I think I may have missed something. I’m also a total beginner and have no idea what I’m doing :) I need this for a project If someone could explain please :)
@clashbluestar3099
@clashbluestar3099 6 лет назад
I love it
@loukask.9111
@loukask.9111 6 лет назад
me too
@mythran1162
@mythran1162 5 лет назад
i completly got lost at i should use i- - or it will give problems.... what did you meant with all that?
@EnglischLernen123
@EnglischLernen123 6 лет назад
good work. thanks
@sim1mk
@sim1mk 6 лет назад
Im going crazy trying move objects on lines (rails). Any suggestions?
@cap-advaith
@cap-advaith 5 лет назад
I love u 3000
@lockedin6853
@lockedin6853 4 года назад
good video man :) thank you
@joshuasamfabre7735
@joshuasamfabre7735 6 лет назад
Thanks for this such a big help ;}
@soundharnatarajan9291
@soundharnatarajan9291 6 лет назад
hi i would like to use p5.js and matter.js with my react front end can any one please provide me some hints of integrating all three together
@alimanoochehri811
@alimanoochehri811 5 лет назад
nice save
@josebarona271
@josebarona271 5 лет назад
What does he mean about the snow??
@studyrelated8925
@studyrelated8925 4 года назад
Is P5js actually used in any real-life production code?
@tomster12
@tomster12 6 лет назад
unrelated to the video but why does he have the p5.js shortcut on his desktop if he is coding in atom? and how is he running/hosting the code if he is using atom? does he keep processing running in the background hosting the js or does he use an inbuilt package to run it?
@TheCodingTrain
@TheCodingTrain 6 лет назад
You can try some of these workflow videos: sublime text: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-UCHzlUiDD10.html atom editor: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-d3OcFexe9Ik.html brackets: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-nmZbhManVcY.html codepen: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-5gfUgNpS6kY.html
@TheNuNoPalma
@TheNuNoPalma 6 лет назад
You rock!!
@dancingbread7015
@dancingbread7015 5 лет назад
*_F O R E A C H_* * dun dun duuuuuuunnnnn *
@baileighdabdoub8343
@baileighdabdoub8343 4 года назад
all the source code is gone from the github??
@xedom
@xedom 6 лет назад
how can i create a deconstructor?
@nickhart8062
@nickhart8062 4 года назад
S P L I C E
@JashJhaveri
@JashJhaveri 2 года назад
lol i was looking for array looping like pythons in and found foreach. I love js because it got me into proper coding thanks to p5 and your videos but that forEach has the weirdest looking syntax ever, and the documentation doesn't help understand whats its actually doing lol.
@tibimose823
@tibimose823 6 лет назад
Hi, I think I'm too late for the party for this video, but I wanted to ask: how do I call the youtube API in javascript with P5? I'm trying to binge watch all the videos on some channels (yours included, *wink*), but I want to see which videos I watched already and which not. Apparently, youtube doesn't help me with that so, for start, I want to get a list. I'll watch some other API videos on this channel, but if anyone has a sample for this, it would be much appreciated.
@TARIKhdG4mer
@TARIKhdG4mer 6 лет назад
Please try codewars
@noizz7857
@noizz7857 6 лет назад
*B U B B L E S*
@theofulis
@theofulis 6 лет назад
Hello, I would like to ask if you ever have the time, could you please make tutorials videos for Java, thank in advance if you can and thank you for the video.
@TheCodingTrain
@TheCodingTrain 6 лет назад
Check of my Processing tutorials, they are actually Java!
@abdurrahmanrobin4915
@abdurrahmanrobin4915 6 лет назад
1:05 minutes , color remove :O why ?
@xerotoninz
@xerotoninz 6 лет назад
arrays start at 1
@michaelblum7168
@michaelblum7168 6 лет назад
the p is silent
@shashidhara.g.m655
@shashidhara.g.m655 4 года назад
Video editor: Let me be the auto correct for now
@netanelberman6291
@netanelberman6291 4 года назад
2: 30
@Hirukaki
@Hirukaki 5 лет назад
can you help me? I made this project - editor.p5js.org/Allayna/sketches/hZgyuRlSL The balls change brightness on mouseover, change color on click, and should remove from array on a doubleclick. However, when I remove one it gives an error - Uncaught TypeError: Cannot read property 'move' of undefined (sketch: line 81)
@irvingdelgado6426
@irvingdelgado6426 6 лет назад
For each Uwu
@kite4792
@kite4792 3 года назад
5:19
@kite4792
@kite4792 3 года назад
lol!
@sir_palma
@sir_palma 6 лет назад
5:15 lol
@ninglizhu2255
@ninglizhu2255 5 лет назад
It REALLY turns to be more complex for me...ZZZ
@SMC4free
@SMC4free 4 года назад
I really don't like developers who make fun of the architecture and design concepts of technologies...there are good use cases and reasons to use forEach buddy :)
@praveenchakravarthy2776
@praveenchakravarthy2776 6 лет назад
Hey Dan.......................what is your age???????
@Muffin--Man
@Muffin--Man 10 месяцев назад
9:50 var bubbles=[]; function setup() { createCanvas(400, 400); for(let i=0;i
@Muffin--Man
@Muffin--Man 10 месяцев назад
With Counter 😁😁 var bubbles=[] ,n=5; function setup() { createCanvas(400, 400); for(let i=0;i=0;i--){ //douple kill //for(let i=0; i< bubbles.length;i){ no overlapping if(bubbles[i].mouseC(mouseX,mouseY)){ bubbles.splice(i,1); n-=1; } } // } } function draw() { background(0); push(); strokeWeight(1); textSize(20); text(n,20,30); pop(); for(let t of bubbles){ t.show(); t.move(); if(t.mouseC(mouseX,mouseY)){ t.Fill(255); }else{ t.Fill(0);} } } class Bubbles{ constructor(x,y,d){ this.x=x; this.y=y; this.d=d; this.f=0; } move(){ this.x+=random(-2,2); this.y+=random(-2,2); } Fill(d){ this.f=d; } show(){ fill(this.f,120) stroke(255); strokeWeight(4); ellipse(this.x,this.y,this.d); } mouseC(u,w){ let d=dist(this.x,this.y,u,w); return d
@seranes_silence
@seranes_silence 3 года назад
*SPLICE
@realcygnus
@realcygnus 6 лет назад
Dan .....the # of times I seen you use spice().....u must too much on your mind there home slice() !
@owennguyen6176
@owennguyen6176 4 года назад
'oh shift'
@UweEichel
@UweEichel 6 лет назад
This was a nice video, but your contains function gave me shivers :-{ just return d < this.r
@nietschecrossout550
@nietschecrossout550 6 лет назад
Slice it, haha
@ColaBeGaming
@ColaBeGaming 6 лет назад
*SPLICE :D
@bcburnettcom
@bcburnettcom 5 лет назад
my Solution In the Bubble Class: clicked(x,y){ if(dist(this.x,this.y,x,y)bubbles = bubbles.filter(el=>el!==e.detail))
@eladkapuza4551
@eladkapuza4551 6 лет назад
forEach 😖
@user-cs1gi6xy3i
@user-cs1gi6xy3i 3 года назад
system.println.out("omg , u are so cute ,I'm in love~")
@ispaze
@ispaze 6 лет назад
Idea: Build Agar.io from scratch :)
@myk3l9675
@myk3l9675 6 лет назад
ispaze um...ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-JXuxYMGe4KI.html
@WolfRose11
@WolfRose11 2 года назад
or at 9:48: return ((d < this.r) ? true : false);
@netanelberman6291
@netanelberman6291 4 года назад
8:06
Далее
7.6: Object Communication Part 1 - p5.js Tutorial
14:00
😱ЖИВОЙ Чехол на Айфон🤪
00:38
Просмотров 257 тыс.
Cristiano Ronaldo Surpassed Me! #shorts
00:17
Просмотров 10 млн
Coding Challenge #107: Sandpiles
21:21
Просмотров 74 тыс.
9.8: Random Circles with No Overlap - p5.js Tutorial
19:25
JavaScript Problem: Comparing Two Arrays without Loops
13:46
7.4: Mouse Interaction with Objects - p5.js Tutorial
14:58
why are switch statements so HECKIN fast?
11:03
Просмотров 407 тыс.
Every Programming Language Ever Explained in 15 Minutes
15:29
Coding Challenge 180: Falling Sand
23:00
Просмотров 896 тыс.
7.3: Arrays of Objects - p5.js Tutorial
14:22
Просмотров 167 тыс.