|
| 1 | +# Removing elements from Arrays |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +*You will learn:* |
| 6 | +- how to remove elements from the end of an Array |
| 7 | +- how to remove elements from the beginning of an Array |
| 8 | +- how to remove elements from an Array after a specific index |
| 9 | +- how to remove a specific element |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +Once you have created an Array, there might be the case that you don’t want to keep |
| 14 | +all of the Array’s elements. This is where some methods for removing elements come |
| 15 | +in handy. |
| 16 | + |
| 17 | +This unit will look into different ways of removing elements from an Array. |
| 18 | + |
| 19 | +## Removing elements from the end of an Array |
| 20 | + |
| 21 | +For removing one ore more elements from the end of an Array you can use the `pop` |
| 22 | +method. The `pop` method modifies the orginal Array and returns the removes elements. |
| 23 | +You can pass the number of elements to remove as argument: |
| 24 | + |
| 25 | +` numbers = [1, 2, 3, 4]` |
| 26 | +` numbers.pop(2)` *# => [3, 4]* |
| 27 | +` numbers` *# => [1, 2]* |
| 28 | + |
| 29 | +If you don’t pass the number of elements to remove it will delete 1 by default: |
| 30 | + |
| 31 | +` numbers = [5, 6, 7, 8]` |
| 32 | +` numbers.pop` *# => 8* |
| 33 | +` numbers` *# => [5, 6, 7]* |
| 34 | + |
| 35 | +Note that, if you remove multiple elements, `pop` will return an Array with the removed |
| 36 | +elements. If you just remove the last element it will return this removed element. |
| 37 | + |
| 38 | +If you try to pop an element from an empty Array, there’s no element left to remove |
| 39 | +and it will return *nil*: |
| 40 | + |
| 41 | +` [].pop` *# => nil* |
| 42 | + |
| 43 | +## Removing elements from the beginning of an Array |
| 44 | + |
| 45 | +For removing elements from the beginning of an Array you can use the `shift` method. |
| 46 | +It takes an optional argument that specifies the number of elements to remove: |
| 47 | + |
| 48 | +` numbers = [0, 1, 2, 3]` |
| 49 | +` numbers.shift(3)` *# => [2, 3]* |
| 50 | +` numbers` *# => [0, 1]* |
| 51 | + |
| 52 | +If you don’t pass an argument it will only remove and return the very first element: |
| 53 | + |
| 54 | +` numbers = [2, 4, 6]` |
| 55 | +` numbers.shift` *# => 2* |
| 56 | +` numbers` *# => [4, 6]* |
| 57 | + |
| 58 | +Similar to `pop`, `shift` called on an empty Array will return *nil*: |
| 59 | + |
| 60 | +` [].shift` *# => nil* |
| 61 | + |
| 62 | +## Removing elements after a specific index |
| 63 | + |
| 64 | +There might be situations where you want to select all elements of an Array after |
| 65 | +a certain position, e.g. all elements after the the third element. |
| 66 | +You can do exactly this with the `drop` method: |
| 67 | + |
| 68 | +` fruits = ['apple', 'pear', 'lemon', 'melon']` |
| 69 | +` fruits.drop(2)` *# => ["lemon", "melon"]* |
| 70 | +` fruits` *# => ["apple", "pear", "lemon", "melon"]* |
| 71 | + |
| 72 | +Apart from `pop` and `shift`, `drop` does not modify the original Array but creates |
| 73 | +a new Array that comprises the dropped elements. |
| 74 | + |
| 75 | +## Removing specific elements |
| 76 | + |
| 77 | +In case you know the elements in an Array, you can specificly remove these. |
| 78 | +There are two similar methods for doing this: `delete` and `-`. |
| 79 | + |
| 80 | +The difference is, that `delete` modifies the original Array, while `-` creates a new |
| 81 | +Array and does not change the original Array. |
| 82 | + |
| 83 | +`delete` take one element to remove from the Array as argument and returns the |
| 84 | +removed elements: |
| 85 | + |
| 86 | +` fruits = ['apple', 'lemon', 'lemon']` |
| 87 | + |
| 88 | +` fruits.delete('lemon')` *# => "lemon"* |
| 89 | +` fruits` *# => ["apple"]* |
| 90 | + |
| 91 | +As you can see, `delete` removes duplicated elements from the Array, here all the |
| 92 | +*'lemon'* elements. |
| 93 | + |
| 94 | +The `-` method takes an Array as argument. It also removes duplicated elements: |
| 95 | + |
| 96 | +` fruits = ['apple', 'lemon', 'lemon', 'melon']` |
| 97 | + |
| 98 | +` less_fruits = fruits - ['lemon']` *# => ["apple", "melon"]* |
| 99 | +` fruits` *# => ["apple", "lemon", "lemon", "melon"]* |
| 100 | + |
| 101 | +The `-` does not modify the original Array but creates a new one. The original |
| 102 | +Array that is stored in *fruits* remains the same. |
| 103 | + |
| 104 | +Before going into the task, let’s summarize all the learned methods for removing |
| 105 | +elements from an Array: |
| 106 | + |
| 107 | +` pop` *removes elements at end modifies the original Array* |
| 108 | +` shift` *removes elements at beginning modifies the original Array* |
| 109 | +` drop` *remove elements after given index creates a new Array* |
| 110 | +` delete` *removes the given element modifies the original Array* |
| 111 | +` -` *removes the given elements creates a new Array* |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +Let’s use our baggage again and unpack a couple of things. |
| 116 | +Define a variable `baggage` and assign an array with all the things you want to |
| 117 | +take with you: *shoes*, *underwear*, *socks*, *kettle*, *glasses*, *shirt*, and *wallet*. |
| 118 | + |
| 119 | +You just realized, that you actually want to wear your shoes (which are the first |
| 120 | +thing in your baggage), so remove them from baggage. |
| 121 | + |
| 122 | +Your friend tells you that you won’t need the kettle, so delete it from your baggage, too. |
| 123 | + |
| 124 | +Last, you think that it’s maybe a better idea to carry your wallet with you. |
| 125 | +Thus, also pop the wallet from your baggage. |
| 126 | + |
| 127 | +Now you’re good to go. Bon voyage! |
| 128 | + |
| 129 | +--- |
0 commit comments