Try Not To Change The Original Array
Instead, if you need to modify or update a collection it is considered better practice to save the changes into a new array rather than of modifying the original collection. If you are new to programming this concept might sound strange. A lot of coders like to write 'clean code' and creating a new collection when it's not needed can be seen as wasteful.
The reason why you should not mutate a collection (fancy word for modify) is that it significantly reduces the chances of bugs occurring. When you create a new collection, it will also make your unit tests easier to write. If you mutate your collection during execution, it can be really difficult to try and spot bugs. If you want to learn a little more about this concept, I suggest you read this article for some extra information.
Iterating through an array is the most common way you will deal with collections of data. You can tell when you are working with an array in JSON as it will be wrapped in square brackets, like so
forEach() can all be used.
Sometimes you may need to deal with a collection of objects, like so:
When dealing with an object, you cannot use the array methods. Instead, you need to use
object.keys to get the name of each objection in the collection. After you have all the
keys within the collection, you can then do a
forEach() using the key to get the data you need. like so: