|
| 1 | +//DATA STRUCTURES: ARRAYS// |
| 2 | + |
| 3 | +///////////////////////////////////////////////////////////////////// |
| 4 | + |
| 5 | +const myArray = []; //shorthand version |
| 6 | +const anotherArray = new Array(); |
| 7 | + |
| 8 | +const tenArray = new Array (10); //specifies the size to be 10 |
| 9 | + |
| 10 | +//method for filling an array with values |
| 11 | +let anArray = new Array(5); |
| 12 | +let val = 1; |
| 13 | +for (const [index, element] of anArray.entries()) { |
| 14 | + anArray[index] = val++; |
| 15 | +} |
| 16 | + |
| 17 | +console.log(anArray); |
| 18 | + |
| 19 | +//reference elements in an array |
| 20 | +console.log(anArray[3]); |
| 21 | +console.log(anArray[1+3]); |
| 22 | + |
| 23 | +////////////////////////////////////////////////////////////////// |
| 24 | + |
| 25 | +//ARRAY METHODS// |
| 26 | + |
| 27 | +let a = [0, 1, 2]; |
| 28 | + |
| 29 | +let len = a.length; //returns the length of the array |
| 30 | +console.log('length:', len); |
| 31 | + |
| 32 | +a.push(8); //adds elements to the end of array |
| 33 | +console.log('push()', a); |
| 34 | + |
| 35 | +a.splice(3, 0, 'cha cha cha'); //adds an element to index:3 |
| 36 | +console.log('splice()', a); |
| 37 | + |
| 38 | +a.pop(); //removes an item from the end of the array |
| 39 | +console.log('pop()', a); |
| 40 | + |
| 41 | +a.shift(); //removes an item from the beginning of the array |
| 42 | +console.log('shift()', a); |
0 commit comments