|
| 1 | +# Adding elements to Arrays |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +*You will learn:* |
| 6 | +- how to add elements to the end of an Array |
| 7 | +- how to add elements to the beginning of an Array |
| 8 | +- how to add elements at a specific index to an Array |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +If you deal with an Array you will hardly just take the Array as is, but you likely want |
| 13 | +to process and modify it or build new Arrays out of it. |
| 14 | + |
| 15 | +This unit will look into different ways of adding elements to an Array. |
| 16 | + |
| 17 | +## Adding elements to the end of an Array |
| 18 | + |
| 19 | +An Array instance has several methods for adding elements. |
| 20 | +If you want to add a *single* element to the end you can use the `<<` method: |
| 21 | + |
| 22 | +` numbers = [1, 2, 3]` |
| 23 | +` numbers << 4` *# => [1, 2, 3, 4]* |
| 24 | + |
| 25 | +You could also write `numbers.<<(4)`, so the missing *.* and *()* are just a bit of |
| 26 | +syntactical sugar. |
| 27 | + |
| 28 | +A similiar method for adding an element to the end of an Array is `push`: |
| 29 | + |
| 30 | +` chars = ['a', 'b']` |
| 31 | +` chars.push('c')` *# => ['a', 'b', 'c']* |
| 32 | + |
| 33 | +The difference is, that `push` accepts multiple arguments, so that you can add as |
| 34 | +many elements as you want in a single call: |
| 35 | + |
| 36 | +` chars.push('d', 'e', 'f')` *# => ['a', 'b', 'c', 'd', 'e', 'f']* |
| 37 | + |
| 38 | +Both `<<` and `push` will modify the given Array directly. If you check the values |
| 39 | +of the variables above after calling the methods on them they will contain the added values: |
| 40 | + |
| 41 | +` numbers` *# => [1, 2, 3, 4] * |
| 42 | +` chars` *# => ['a', 'b', 'c', 'd', 'e', 'f']* |
| 43 | + |
| 44 | +If you want to build a new Array out of an existing one by adding elements, the `+` method |
| 45 | +will help you to combine two Arrays: |
| 46 | + |
| 47 | +` numbers = [1, 2]` |
| 48 | + |
| 49 | +` more_numbers = numbers + [3]` *# => [1, 2, 3]* |
| 50 | +` even_more_numbers = numbers + [-1, -2]` *# => [1, 2, -1, -2]* |
| 51 | + |
| 52 | +The original Array will stay the same: |
| 53 | + |
| 54 | +` numbers` *# => [1, 2]* |
| 55 | + |
| 56 | +## Adding elements to the beginning of an Array |
| 57 | + |
| 58 | +If you want to add elements to the beginning of an Array instead, you can use the |
| 59 | +`unshift` method which takes 1 or more elements to add as arguments: |
| 60 | + |
| 61 | +` numbers = [1, 2]` |
| 62 | +` numbers.unshift(0)` *# => [0, 1, 2]* |
| 63 | +` numbers.unshift(-2, -1)` *# => [-2, -1, 0, 1, 2]* |
| 64 | + |
| 65 | +Similar to `push`, the `unshift` method will modify the original Array. |
| 66 | + |
| 67 | +## Addding elements at a certain index |
| 68 | + |
| 69 | +If adding an element to the end or the beginning of an Array is not what you want, |
| 70 | +then there is an `insert` method, that allows you to insert one or more elements |
| 71 | +at a defined position: |
| 72 | + |
| 73 | +` vegetables = ['potato', 'pepper']` |
| 74 | +` vegetables.insert(1, 'cucumber', 'cauliflower')` |
| 75 | + |
| 76 | +The first argument is the index where to insert the first element (keep in mind, that |
| 77 | +the indexing starts at 0, so *1* means: “Insert before the second element”). |
| 78 | + |
| 79 | +Like `<<` and `push`, `insert` also modifies the original Array, so that *vegetables* holds |
| 80 | +all the values now: |
| 81 | + |
| 82 | +` vegetables` *# => ['potato', 'cucumber', 'cauliflower', 'pepper'] |
| 83 | + |
| 84 | +Now that you have learned a couple of possibilities for adding elements to an Array |
| 85 | +let’s summarize them again: |
| 86 | + |
| 87 | +` <<` *adds element at end modifies the original Array* |
| 88 | +` push` *adds elements at end modifies the original Array* |
| 89 | +` +` *adds elements at end creates a new Array* |
| 90 | +` unshift` *adds elements at beginning modifies the original Array* |
| 91 | +` insert` *adds elements at given index modifies the original Array* |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +Prepare a case with some things for a trip: |
| 96 | + |
| 97 | +Create a variable *baggage* holding an Array with one element *"shirt"*. |
| 98 | +Add a pair of *"glasses"* before the shirt and some *"shoes"* after the shirt. |
| 99 | +Then add another *"trousers"* and a pair of *"socks"* before the shoes. |
| 100 | + |
| 101 | +--- |
0 commit comments