Professional Documents
Culture Documents
13 Useful JavaScript Array Tips and Tricks You Should Know - DEV Community ? ?? ?
13 Useful JavaScript Array Tips and Tricks You Should Know - DEV Community ? ?? ?
// First method
var uniqueFruits = Array.from(new Set(fruits));
console.log(uniqueFruits); // returns [“banana”, “apple”, “orange”, “wa
// Second method
var uniqueFruits2 = […new Set(fruits)];
console.log(uniqueFruits2); // returns [“banana”, “apple”, “orange”, “w
Easy, right?
var friends = [
{ name: ‘John’, age: 22 },
{ name: ‘Peter’, age: 23 },
{ name: ‘Mark’, age: 24 },
{ name: ‘Maria’, age: 22 },
{ name: ‘Monica’, age: 21 },
{ name: ‘Martha’, age: 19 },
]
4. Empty an array
7. Merge arrays
Do you know how to merge arrays into one array not using
.concat() method? There is a simple way to merge any
amount of arrays into one in one line of code. As you
probably realized already spread operator (…) is pretty
useful while working with arrays and it’s the same in this
case.
It’s also one of the most popular challenges which you can
face on any Javascript interview because it shows if you can
use array methods and what is your logic. To find the
intersection of two arrays, we will use one of the previously
shown methods in this article, to make sure that values in
the array we are checking are not duplicated and we will use
.filter method and .includes method. As a result, we will get
the array with values that were presented in both arrays.
Check the code:
var mixedArr = [0, “blue”, “”, NaN, 9, true, undefined, “white”, false
var trueArr = mixedArr.filter(Boolean);
console.log(trueArr); // returns [“blue”, 9, true, “white”]
Conclusion
Duomly + FOLLOW
We believe everyone can learn how t o code, so we are making learning fun and easy!
@duomly duomly www.duomly.com
Add to the discussion
PREVIEW SUBMIT
Oct 21
willsmart
Tip 10 has an off-by-one error going on: t here's no need to add one to colors.length since it 's
already one more t han t he highest index.
Great t ips t hough! I didn't know about t he array.length = 0 one and will use it for sure.
5 REPLY
Oct 21
Duomly
3 REPLY
Oct 21
gabriel-pinheiro
A nice addit ion would be to change t he 8. Find the intersection of two arrays to do it t he
ot her way around as Array.prototype.includes is linear and Set.prototype.has is const ant , for
larger list s a big difference can be not iced:
3 REPLY
Oct 21
Alex K.
Nice list !
Point 3 is more useful when applied to array-like object s, e.g. NodeList , since you can t ransform
t hem into arrays and map t he it ems to new values at t he same t ime.
2 REPLY
Oct 21
Adam Culpepper
“ and ” need to be convert ed to regular double quot es (") or single quot es so your code can be
copy/past ed into t he console wit hout having to convert t hem all.
t he charact er you used for t he spread operator (…) needs to be convert ed to t hree periods (.) for
copy/past e purposes. I t hink it 's also import ant to not e in t he art icle t hat t he spread operator is
ES6.
grammar/word errors:
"Get random value form t he array" t it le > needs to be "from array"
2 REPLY
Oct 21
Maxence Bouret
1 REPLY
Oct 21
premierboy
Hello ...i want to learn js can someone share wit h me a t utorial or course
2 REPLY
Oct 21
Jacob Hanko
You will be able to find all different kinds of t utorials out t here for JavaScript , ranging from
books to video courses. Here are a few to get you st art ed, but I suggest finding a t utorial t hat
complement s your own personal learning st yle.
Javascript .info
10 Websit es to Learn JavaScript for Beginners
Learn JS
4 REPLY
Oct 21
premierboy
Thanks
1 REPLY
Oct 21
minhpn76
Great !
2 REPLY
Oct 21
Kevin Silvestre
2 REPLY
Oct 21
K-Sato
Awesom!!
2 REPLY
Oct 21
Nicholas Ferrer
2 REPLY
If you could wave a magic wand and make your biggest challenge when you were
lear...
109 91
Peacock now has integration with the Live Share VS Code extension.
53 3
A Refactoring Breakdown
Jennifer Lyn Parsons - Oct 21
Cypress vs.....
Ben Halpern - Oct 21