CodingTrain / Suggestion-Box

A repo to track ideas for topics
570 stars 86 forks source link

p5.js tutorial - advanced arrays (jagged 2d array of objects) #397

Open CDArena opened 7 years ago

CDArena commented 7 years ago

A follow up to tutorial 9.12: 2D Arrays in JavaScript

Data doesn't always come in neat 4x7 element packages; sometimes it is irregular. I'm not entirely sure what the represented limits are in p5.js - I believe you can make an array of arrays, but how about when dimensions get jagged? Or an array of jagged arrays of objects?

Consider one example: gift boxes under a Christmas tree. A box might be represented as an object with several properties -

You might want to organize them several ways - the simplest is an array of all boxes, but what if you wanted an array of Senders, and each element of this array contained an array of boxes sent by them.

Would this be possible to represent? How would one enumerate the various arrays, or get the lengths?

What would the syntax be for the length of a Sender list?

If boxes[sender][item] was the syntax for one item, how would one get the number of items from a sender? How would one get the number of senders?

Could this data be extended to include an item name and cost? These things periodically haunt my dreams...

HaydnG commented 7 years ago

I'm not quite sure i understand what you're asking.

with this list boxes[sender][item] You can do things like boxes.length for the amount of senders. And boxes[sender].length for the amount of items a sender sent.

And each item in the list will be an object that stores its name and cost.

Please tell me if i mis-understood what you were asking completely.