4. Arrays & OOP


Arrays and Classes

Classes / Objects




Arrays

›An array is a list of data. Each piece of data in an array is identified by an index number representing its position in the array. Arrays are zero based, which means that the first element in the array is [0], the second element is [1], and so on. In this example, an array named "coswave" is created and filled with the cosine values. This data is displayed three separate ways on the screen.‹ (https://p5js.org/examples/arrays-array.html)

var person = {
  firstName: “John”,
  lastName: “Doe”,
  age: 20
 };

The first element of an array is at index 0, the last element is at the index corresponding to the value of the length property of the array minus 1. Using an invalid index number returns undefined.

Make an array

let myArray = []; 
  var fruits = ['Apple', 'Banana'];

Access an array

var first = fruits[0];
  // Apple

Getting the Size of an Array

numbersArray.length;

Looping an Array

let numbersArray = [1, 2, 3, 4, 5];
  for(let i=0; i < numbersArray.length; i++) {
    console.log(numbersArray[i]);
  }

Insert elements into an array

var newLength = fruits.push('Orange');

Delete an element at a particular index

var removedItem = fruits.splice(pos, 1); // this is how to remove an item

Two-dimensional arrays

See the example on reading the pixel matrix to obtain color information.




Storing obstacles objects in arrays.



Using FFT to generate obstacles from sound