DEV Community

Cover image for 28 Javascript Array Methods: A Cheat Sheet for Developer

28 Javascript Array Methods: A Cheat Sheet for Developer

Rahul Sharma on March 28, 2022

Let's understand javascript array functions and how to use them. Array.map() Returns a new array with the results of calling a...
Collapse
 
konrud profile image
Konstantin Rouda

Array flat example has a wrong result.

Applying Array.flat() without any value will only flat a provided array up to one level. In the provided example you have an array with 2 nesting levels. Thus, the result should be [1, 2, 3, 4, [5, 6]].

If you know the number of nesting arrays you can provide it (e.g. list.flat(2)), otherwise you can use global property Infinity with it (e.g. list.flat(Infinity)) which will flat all number of nesting arrays.

Thus your example might be rewritten in the following way


// Code
const list = [1, 2, [3, 4, [5, 6]]];
list.flat(Infinity); // [1, 2, 3, 4, 5, 6]

// OR
list.flat(2); // [1, 2, 3, 4, 5, 6]

Enter fullscreen mode Exit fullscreen mode
Collapse
 
devsmitra profile image
Rahul Sharma

My bad, Thank you so much for pointing it out.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

As previously mentioned, these are not hacks. Might also be a good idea to give working code samples, instead of ones with emojis that give errors.

Collapse
 
devsmitra profile image
Rahul Sharma

Thanks for the suggestion, Emojis will work if use as string '😊'.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

But not as variable names

Thread Thread
 
devsmitra profile image
Rahul Sharma

I've also add code snippets, hope that helps.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Cool article, but poorly named. These just aren't hacks.

Collapse
 
devsmitra profile image
Rahul Sharma

Thanks for the suggestion

Collapse
 
oricohen profile image
OriCohen05

Nice article, the emoji's are bad for me, sorry.

Collapse
 
devsmitra profile image
Rahul Sharma

Thanks, I've also add code snippets, hope that helps.

Collapse
 
oricohen profile image
OriCohen05

It is man, Good job ! (:

Collapse
 
tmchuynh profile image
Tina Huynh

Love it!! Thanks for sharing

Collapse
 
gtobar profile image
Guillermo Tobar

Good idea, adding emojis worked for me, it was also clear and simple. Thanks for posting.

Collapse
 
qq449245884 profile image
qq449245884

Dear Rahul Sharma,may I translate your all dev articles into Chinese?I would like to share it with more developers in China. I will give the original author and original source.

Collapse
 
devsmitra profile image
Rahul Sharma • Edited

Sure, you can don't forget to add original author and original source.

Collapse
 
elsyng profile image
Ellis

Interesting. One can read the code with emojis very fast. Very good idea :)

Collapse
 
gauravvishwakarma profile image
Gaurav Vishwakarma

At only one glance I got all the methods. It's really awesome 👌. Unique concept of explaining with emojis.

Collapse
 
harsh_dodiya profile image
Harsh Dodiya

Helped me a lot, thanks for sharing 🙌

Collapse
 
katesoft8 profile image
Kate Soft

Good text.
I'm new in developing, and your article saved me a lot of time. I save it to my bookmarks)
I'm trying to find a job offer for React js, could you please take a look if those questions keenethics.com/blog/react-intervie... for interview are actual?

Collapse
 
devsmitra profile image
Rahul Sharma
Collapse
 
timhuang profile image
Timothy Huang

Useful cheat sheet, emojis are so funny to show off these functions. Thanks for sharing.

Collapse
 
aruls1962 profile image
aruls1962

A two-dimensional array example for each section will be useful. No one deals with a single dimension array in real life

Collapse
 
dannyengelman profile image
Danny Engelman

A somewhat shorter sheet:
array-methods.github.io

Collapse
 
fires3as0n profile image
fires3as0n

Since when copying documentation is a Cheat Sheet?
In Cheat Sheet methods should be grouped and seen in one screen.

Collapse
 
golangch profile image
Stefan Wuthrich

Cool. Prepared like this it should be easy to find a #react job on reactjsjob.com or javascriptjob.xyz

Collapse
 
ardiaankur profile image
Ankur Gupta

How come these are hacks? These are in built JavaScript methods available on mozilla JavaScript documentation as well.

Collapse
 
devsmitra profile image
Rahul Sharma

I've already updated the title.

Collapse
 
oricohen profile image
OriCohen05

Otherwise, I would love if you'll go to check out my last article and give me an advise or any tips on changes.