Simon Hรธiberg Profile picture
Building a portfolio of bootstrapped SaaS products.

Aug 5, 2021, 16 tweets

JavaScript ๐Ÿ’ก

14 Array Methods that will land you the next job.
Learn and understand what they do and how to use them.

Explanation and examples below ๐Ÿงต๐Ÿ‘‡

๐Ÿ”ธ map

The map() method creates a new array populated with the return value of the callback function for each element in the array.

๐Ÿ”ธ forEach

The forEach() method executes a provided function once for each array element.
The callback function does not expect a return value, and the forEach() method itself also returns undefined.

๐Ÿ”ธ filter

The filter() method creates a new array containing only the elements that "pass the test" implemented by the callback function.
We call this type of callback a predicate function.

๐Ÿ”ธ find

The find() method behaves similarly to the filter() method, but it only returns a single element.
This method will stop at the first element that "pass the test", and return that.
If none exists, it will return undefined.

๐Ÿ”ธ findIndex

The findIndex() method behaves similarly to the find() method, but it returns an index instead of the element,
This method will stop at the first element that "pass the test", and return its index.
If none exists, it will return -1.

๐Ÿ”ธ reduce

The reduce() method takes a callback with (at least) two arguments:
An accumulator and the current element.

For each iteration, the return value of the callback function is passed on as the accumulator argument of the next iteration.

๐Ÿ”ธ some

The some() method takes a predicate function and return true if any of the elements in the array "passes the test".

๐Ÿ”ธ every

The every() method takes a predicate function and returns true if all of the elements in the array "pass the test".

๐Ÿ”ธ includes

The includes() method checks if an array includes a certain value among its elements, returning true or false.

๐Ÿ”ธ fill

The fill() method replaces all the elements in an array to a given value.

๐Ÿ”ธ reverse

The reverse() method reverses the order of the elements in the array.

๐Ÿ”ธ flat

The flat() method creates a new array with all sub-array elements flattened into it.
You can specify a depth. The default is 1.

๐Ÿ”ธ flatMap

The flatMap() method applies a callback to each element of the array and then flatten the result into an array. It combines flat() and map() in one function.

๐Ÿ”ธ sort

The sort() method is used to sort the elements of an array and returning the sorting array.
Be aware that this method is mutating the original array.

I recently published an e-book with 65+ code-snippets including explanations for JavaScript, React, NodeJS, and AWS Lambda.

Pick it up for โšก free โšก here on Gumroad ๐Ÿ‘‡

๐Ÿ”— simonhoiberg.gumroad.com/l/65-js-codesnโ€ฆ

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling