This post will explain how to interact with array data using array methods. You will learn about different methods and how they work. You will also see some examples of the syntax and an explanation of the behavior. Finally, you will see some code examples of ways to use array methods in practical situations.
Let’s dive in.
Let’s break this down, when you create an array, the process uses the class blueprint to create an instance of the class. An array instance is an array you create that has access to the static methods and other methods as well.
For example, one static method from the array class is called isArray(), which identifies arrays and belongs to the array class. However, the concat() method does not belong to the array class and is used on several different data types.
Now that you have an understanding of what the above methods do, it’s a good time to look at some examples of how they behave. Below is a list of examples, one example for each of the above methods.
const nums = [1, 2, 3]; console.log(nums.reverse()); /*Would return array with values in reverse order*/
const array = [, , , , ] const a = array.flatMap(arr => arr * 10) /*With .flat() and .map(), identical in behavior to the above*/ const b = array.flat().map(arr => arr * 10) /*Both would return [10, 20, 30, 40, 50]*/
Moving forward you can use that cheat sheet as a reference point to practice different tasks with, you can also add to it. A great way to keep track of those neat little tricks and tips is to create cheat sheets for different needs. Practice and explore and you will begin committing things to memory without even knowing it.
Originally published Jun 29, 2022 7:45:00 AM, updated June 29 2022