DEV Community

Cover image for 14 Awesome JavaScript Array Tips You Should Know About

14 Awesome JavaScript Array Tips You Should Know About

Kai on December 09, 2020

This post was originally published at kais.blog. Let's move your learning forward together! Follow me on Twitter for your daily dose of developer ...
Collapse
 
jonrandy profile image
Jon Randy 🎖️

An easier way to copy an array:

const fruitsA = ["🍎", "🍌", "🍒"];
const fruitsB = [...fruitsA];
Enter fullscreen mode Exit fullscreen mode
Collapse
 
picwellwisher12pk profile image
Amir Hameed

I was also looking if this method is mentioned in the article.

Collapse
 
olvnikon profile image
Vladimir

Thank you for the nice tips!

How to Remove All Falsy Values From an Array

You can also use simple coercion to Boolean

const fruits = ["🍎", false, "🍌", undefined, "🍒"];

const filteredFruits = fruits.filter(Boolean);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hilleer profile image
Daniel Hillmann

Was about to suggest this as well. Much cleaner to me :-)

Collapse
 
kamcio profile image
kamcio • Edited

A screenshot of JavaScript code with Replacement Glyphs in place of some emojisPlease don't use emojis.
When your browser doesn't render emojis you get Replacement Glyph.

Collapse
 
kais_blog profile image
Kai

Thanks for your feedback! What browser are you using?

Collapse
 
kamcio profile image
kamcio

Latest Chrome on Win7 with almost all fonts deleted.

Collapse
 
szymondziewonski profile image
Szymon Dziewoński • Edited

You should probably write that copying array/object with spread operator/slice/object.assign/json technique is just shallow copying - which is important information. Also you can use Object.assign([], [your array]) and another one JSON.parse(JSON.stringify([your array])) (but I would not recommend it to you) :)

Collapse
 
thelogicwarlock profile image
Kaemon Lovendahl

You can also do [1, 2, 3, 4, null, 5, undefined].filter(Boolean) to remove falsey values.

Collapse
 
ebrima2ray1 profile image
Ebrima Touray

Nice one! Thanks

Collapse
 
codejackdev profile image
Code Jack

awesome tips

Collapse
 
panshak profile image
Panshak

Absolutely amazing. I've started learning react and I noticed that I'll be dealing with arrays of different sorts. This article is quite helpful.