Seven JavaScript array functions which ought to be inbuilt but you will write yourself

This blog post will explore array functions which when I started learning JavaScript I expected to be inbuilt but are not. All of them have an equivalent PHP function since it was my first language. The post is beginner-friendly and most of the content will not be very useful for an advanced developer.

Numerical sorting arrays

Since the inbuilt array method sort () will sort array elements alphabetically, it will not work well with numbers. since, for example, when sorting in ascending order 100 will come before 42 since 1 as a string comes before 4. This how we can adapt the sort () method to sort numeral array elements in ascending and descending order.

Ascending sort

var ages = [12, 13, 5, 5, 65, 0];
function ascendingSort(array){
array.sort(function (a, b) {
  return a - b;
});
 return array;
}
console.log(ascendingSort(ages));

In the above code, we use a compare function with sort. This is the function I am talking about

function(a, b){
Return a-b
}

This function will take two array elements a and b and compare them. If a-b is negative meaning a is smaller than b then nothing happens since it is already in ascending order. If a-b is positive, meaning a is larger then a swap happens. If a-b is 0 nothing happens since they are equal.

Descending sort

Descending numerical sort will follow the same implementation with a compare function. The only difference is that we will be comparing b-a, not a-b. Below is a code sample for the same

var ages = [12, 13, 5, 5, 65, 0];
function descendingSort(array){
array.sort(function (a, b) {
  return b - a;
});
 return array;
}
console.log(descendingSort(ages));

Array maximum

To get the maximum number in an array of numerical elements we make use of Math.max() inbuilt function and the spread operator. We will use the spread operator to make all the individual elements of the array as arguments to Math.max(). You can learn more about the spread operator here. Below is an example code of how it can be done

var ages = [12, 13, 45, 5, 65,65, 0];
function arrayMax(array){
return Math.max(...array)
}
console.log(arrayMax(ages));

There is also a second way where we can make use of apply () method together with Math.max() to achieve the same result. Below is the code for the same:

var ages = [12, 13, 45, 5, 65,65, 0];
function arrayMax(array){
return Math.max.apply(null, array);
}
console.log(arrayMax(ages));

Array minimum

As you would expect for finding the minimum element in an array, we are going to have the same implementation. The difference is that we are now going to use Math.min() instead. Below are code samples of the same:

With spread operator:

var ages = [12, 13, 45, 5, 65,65, 0];
function arrayMin(array){
return Math.min(...array)
}
console.log(arrayMin(ages));

With apply()

var ages = [12, 13, 45, 5, 65,65, 1];
function arrayMin(array){
return Math.min.apply(null, array);
}
console.log(arrayMin(ages));

Array Unique

When working with arrays sometimes you might want to eliminate duplicates. Let us see how we can implement arrayUnique() function in JavaScript.

var a = ['a', 1, 'a', 2, '1','5',5];
function arrayUnique(array) {
    var uniqueArray=[];
    for(let i=0; i<array.length; i++){
       if(array.indexOf(array[i])===i){
       uniqueArray.push(array[i]);
       }
    }
    return uniqueArray;
}
console.log(arrayUnique(a));

The above code using a for loop is how you can get unique elements from an array. The example differentiates even different types, for example, 1 as a number and ‘1’ as a string are two unique elements. In the above function this check

array.indexOf(array[i])===i

allows us to filter out the duplicates. This is how it works. The array function array.indexOf(val) returns the index of the first occurrence of a value in an array. So, if there are duplicates only the first element passes the test. For example, in the case above the index of the second ‘a’ is 2 but the index of the first occurrence of ‘a’ is 0 so 0==2 is false therefore second ‘a’ is filtered out.

Array Sum

For this one, we can use reduce () function to reduce the array to the sum of its elements. I am going to use an arrow function with reduce() so that I do have to write two functions. Here is the sample code:

var numbers=[0,5,5,5];
function arraySum(array){
    var sum=array.reduce((total,num)=>{
     return total+num
    })
    return sum
}
console.log(arraySum(numbers));

This can also be done easily with a for loop

var numbers=[0,5,4,5];
function arraySum(array){
    var sum=0;
    for(let i=0; i<array.length; i++){
       sum=sum+array[i];
    }
    return sum;
}
console.log(arraySum(numbers));

Array Product

You might want to get the product of all the elements of an array and the implementation is similar to array sum, only the operation sign changes. Here are the sample codes:

With reduce()

var numbers=[2,5,6];
function arrayProduct(array){
    var product=array.reduce((total,num)=>{
     return total*num
    })
    return product
}
console.log(arrayProduct(numbers));

With for loop:

Here we also need to initialize the variable product with 1 instead of zero

var numbers=[5,4,5,2];
function arrayProduct(array){
    var product=1;
    for(let i=0; i<array.length; i++){
       product= product *array[i];
    }
    return product;
}
console.log(arrayProduct(numbers));

I will be making another article if I find other array functions I use repetitively in JavaScript but are not inbuilt. So Visit again. Have a good day!