7.5: Removing Objects from Arrays – p5.js Tutorial

[ad_1]
This video covers the “splice” function to delete / remove objects from an array.

Next video:

Support this channel on Patreon:
To buy Coding Train merchandise:
To Support the Processing Foundation:

Send me your questions and coding challenges!:

Contact:
Twitter:
The Coding Train website:

Links discussed in this video:
Array.prototype.splice() reference on MDN:
Simple Particle System Coding Challenge:

Source Code for the all Video Lessons:

p5.js:
Processing:

For an Intro to Programming using p5.js:
For Coding Challenges:

Help us caption & translate this video!

Comments

17 responses to “7.5: Removing Objects from Arrays – p5.js Tutorial”

  1. MCMuet Avatar

    Shiffman? No… SHIFTMAN!

  2. Frits van Doorn Avatar

    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.

  3. Alberto Calabrese Avatar

    I find foreach so useful, why people is against it? I'm a newbie in javascript xD

  4. René Carannante Avatar

    Could you add a tendency in the move function to move less to the e.g. top when its near to the top?

  5. Fly Kasual Avatar

    This was a nice video, but your contains function gave me shivers :-{ just return d < this.r

  6. Ken Haley Avatar

    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.

  7. realcygnus Avatar

    Dan …..the # of times I seen you use spice()…..u must too much on your mind there home slice() !

  8. MisterMajister Avatar

    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.

  9. Sebastian Scholle Avatar

    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'.

  10. Praveen Chakravarthy Avatar

    Hey Dan…………………..what is your age???????

  11. Ronish Joshi Avatar

    Hey daniel ..guess my age?? i'm 13 and i'm stuck in thinking about should i continue learning programming or i should focus on my study now…
    .. please give me your opinion..

  12. Jaidan Balea Avatar

    * SPLICEEEEEEEEEEEEEEEEEE

  13. ispaze Avatar

    Idea: Build Agar.io from scratch 🙂

  14. noizz Avatar

    B U B B L E S

Leave a Reply

Your email address will not be published. Required fields are marked *