Practical Javascript Array Cheat Sheet

Yogi Saputro
3 min readAug 9, 2020
Array of Soldiers (Photo by Maria Oswalt on Unsplash)

Javascript is undoubtedly widespread by now. Web is full of them. It is getting more accessible as well. Plenty of resources are available for anyone willing to learn. Tutorials, courses, forums, books, boot camps, all tried to make us able to make a working application from scratch.

But in my experience as mentor at Binar Academy, those jungle of knowledge is overwhelming for some. So I figured out that there should be condensed version of learning material. Furthermore, it should be both conceptual and practical. Javascript array is fine example. We use them a lot. So in this article, I assembled a list of common tasks of Javascript array manipulation. Hopefully, this can be helpful for Javascript developers.

Please do mind that this article will not mention built-in Javascript array functions. You can find that here or here. I’m talking about finding maximum value of an array, deleting duplicates, etc. Practical things useful in many projects.

As convention, I’m going to use regular function declaration with array(s) and some parameter. The output might be single value, an array, or arrays. Now, please enjoy my assortment of practical use cases using Javascript array.

Maximum/minimum value of Javascript array

Given an array of numbers, find the maximum or minimum value of an array.

// get minimum value of an array
function minValueArray(arr) {
return Math.min.apply(null, arr);
}

// get maximum value of an array
function minValueArray(arr) {
return Math.max.apply(null, arr);
}

Longest element in Javascript array of strings

Given an array of strings, find the longest element (i.e most character count). There is possible case where there are more than one longest elements. We will separate algorithm for each case below.

// get the FIRST longest element in array of strings
function longestElement(arr) {
return arr.sort((a, b) => { return b.length - a.length; })[0];
}

// get ALL longest elements in array of strings
function longestElements(arr) {
let maxLength = arr.reduce((a,b) => a > b.length ? a : b.length, 0);
return arr.filter(element => element.length == maxLength);
}

Select Random Element in Javascript Array

Given an array, return a random element in that array.

// get random index of an array
function getRandomIndex(array) {
return Math.floor(Math.random()*array.length))
}

// get random element of an array
function getRandomElement(array) {
return array[Math.floor(Math.random()*array.length))]
}

Finding elements that match search term in array of strings

Given an array and search term, return new array containing elements that match the search term.

// get the index of FIRST match
function findString(term, arr) {
for (var i=0; i<arr.length; i++) {
if (arr[i].match(term)) return i;
}
return -1;
}

// get the elements of ALL matches, return -1 if not found
function findAllString(term, arr) {
let result =[];
for (var i=0; i<arr.length; i++) {
if (arr[i].match(term)) {
result.push(arr[i]);
};
}
if (result.length > 0) {
return result;
} else {
return -1;
}
}

Javascript array parameter validation

Given a function with parameter, checks if it’s an array. Return true (or continue) if it is an array, otherwise stop.

function stepsWithArrayValidation(arr) {
if (!Array.isArray(arr)) return -1;
return true; // or continue your code
}

Remove duplicates or get distinct elements in Javascript array

Given an array, return new array with duplicated elements removed, leaving it as array of distinct values. Personally, I like the elegance of this approach.

// return array of distinct elements, no more duplicates
function removeDuplicates(arr) {
return [...new Set(arr)]
}

There’s more to come. I’ll keep up with more use cases later. If you have feedback or ideas, please don’t hesitate to tell me.

I am Yogi Saputro, a full stack developer and mentor at Binar Academy, a tech company focused on developing high quality engineering talents. I’m also passionate in data science and business. Check out my other stories to find out more.

--

--

Yogi Saputro

Software developer with MBA degree, mentor, somewhat fatherly figure, data and business synergy enthusiast