Just so you know: Uncommonly used Array Methods

Published by Rienz Otiong on

Welcome to the second episode of the “Just so you know” series where we talk about Array Methods in JavaScript.

For those who are new here in this blog, this series talks about things that some of you might not know and some already know and using what the article is talking about.

 

Now I’m going to introduce to you some uncommonly used array methods which the popular once are map, reduce and filter.

 

 

Reverse

We will start with one of the most simplest and useful method which is the reverse function which reverse the position of the array making the first array element becomes the last, and the last array element becomes the first.

Some of the use case of this method is when you have a sorted list of posts from oldest to latest and you need to display it on a table from the latest posts to the oldest. Also take note that be careful on using this as it mutates/reverse also the original array.

 

 

Every

every is used to check whether all elements in the array pass the test implemented by the provided function and would return a Boolean value.

Just like the example, the use case of this method is that if you need to check all of the items of the array whether all of them meet your requirements or not.

 

 

Some

some is somewhat related with every however the difference is it only requires one to pass the test and not all of them.

So like if you have a booking app and it needs to check whether the room still have an available time slot.

 

 

Shift & Unshift

If we have push and pop we also have shift and unshift which do the same thing but targets the first element of the array.

This is one of the useful method when you want to make your newly created object into the beginning of your list.

 

 

Includes

includes is a method determines whether an array includes a certain element with the same type.

Going back with the booking app. includes can check if the specific time slot is available or not.

 

 

Reduce Right

reduceRight is same as reduce function but instead it starts iterating from right to left. so rather than using reverse then reduce, you could straight using reduceRight.

 

 

Copy Within

copyWithin method copies part of an array to the target index in the same array and returns it, without modifying its size.

 

 

Flat (Experimental)

flat is still an experimental API that should not be used in production code but still worth the time to check because its very useful in the future if ever.

flat method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. The depth by default to 1 which flattens the first layer of nested arrays on the array.

 

 

Flat Map

flatMap is similar to flat and related to map in which it maps the array then flattens it afterwards.

 

 

Extra

Do you know that doing for loop is more faster than map? So for having a large size array then a simple logic like adding one to every item. Let’s see the difference.

The results are:

reduce method has a similar output

reduce vs forloop results:

Conclusion

In the end. its really the use case when to use this methods. if you have a pretty small set of data then its preferred to use this short and readable methods rather than using forloops however if we are talking about large amount of data and we need the best performance then maybe forloop would be your best bet.

To see more available Array Methods in JavaScript you could checkout this link.

For more “Just so you know” posts. Just click here


Leave a Reply

%d bloggers like this: