JavaScript 30 — Day 4 — Array Cardio 1

Steven Chen
2 min readNov 30, 2021

--

No preloaded HTML markup or CSS to make a nice visual. Today’s challenge was all about practicing Array methods.

It’s always good to brush up on your Array methods.

The challenge covered:

  • map
  • sort
  • filter
  • reduce

forEach wasn’t used but it is the foundational piece so you shouldn’t need any other practice with that.

I was familiar with the methods in this challenge but I did learn a couple of tricks or different ways of thinking.

For me the best practice came with reduce. I have used it but not as much as I would like and it’s great to get as much exposure to it as possible.

Things I learned:

  • One of the exercises required a conversion from a NodeList to an Array. While I know how to use the ES6 spread operator to make the conversion, the older Array.from() method also works in this case. It is perhaps more syntactically logical but I’m a fan of the former since it’s a little shorter and provides some other flexibility.
  • I had never really thought to throw an object as a parameter in a .reduce() for whatever reason. The method just takes in a parameter so it’s no different than what you can pass to a regular function, it had just never occurred to me.

Interesting things to note:

For .sort Wes kept using -1 and 1 and a ternary, whereas you only really need any positive or negative number (or 0 but that’s different scenario).

This is a good catch all and perhaps good practice but you would only need that if you were not dealing with numbers.

Example:

newArray.reduce((a,b) => a-b > 0 ? 1: -1)

Where as you could also do it straight up, as long as a and b are numbers

newArray.reduce((a,b) => a-b)

--

--

No responses yet