JavaScript Essentials: Arrays

Essentials is a series that covers the most used and important methods for X topic. It’s a series for developers who know another language or someone who wants a quick start. In this post we cover Arrays.

Prerequisites

It’s suggested that you know about Arrow Functions and Ternary expressions before reading.

Array Basics

You should know what Arrays are, but just in case here’s a quick overview. Arrays are like boxes that contain stuff inside them, you can remove items, add items or manipulate them. They’re like lists in Python or Arrays in most languages.

Array Creation

Here are a few ways to create Arrays.

Array Creation

Adding / Removing Basics

push pop shift splice

Common Methods

An overview of common scenarios and the method to use for each scenario. You’ll notice most methods that take a callback function have the same parameters: ( element, index, array )

Below are code blocks with a scenario/task described in a comment up top then some code below.

Modify Every Element in the Array

Array.map

Copy Without Mutating

slice, spread

Find the Element(s)/Index

find, findIndex,

Is this Element in the Array?

includes

No Duplicates

unique sets

Does Every Item in the Array fulfill these Requirements?

Array.every

Do Some of the Items in the Array fulfill these Requirements?

Array.some

Populate an Array of Emptiness

Array.fill

Wtf Sort?

Array.sort

How to Reverse a String?

split, reverse, join

Flatten an Array

Experimental Array.flat

Convert this Array into an Object

Array.reduce

Loopy Loops

for in, for of, Array.entries

References

Thanks for reading! Leave any feedback in the comments and let me know if there’s a better way to structure these posts ( updated to use Carbon instead of Medium code blocks )

 

SOURCE: https://codeburst.io/javascript-essentials-arrays-2d275b9598c5

Written by

Go to the profile of CodeDraken

CodeDraken

#{ Something impressive here }

 

codeburst

codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

 

Scroll al inicio

Si continuas utilizando este sitio aceptas el uso de cookies. más información

Los ajustes de cookies de esta web están configurados para "permitir cookies" y así ofrecerte la mejor experiencia de navegación posible. Si sigues utilizando esta web sin cambiar tus ajustes de cookies o haces clic en "Aceptar" estarás dando tu consentimiento a esto.

Cerrar