How to Sum, Filter, and Shuffle array values in JavaScript?

In this article, we showed easy tricks for arrays that help to get the task done in a simple way.

Sum All Values of Array

During development in some cases, we have to sum all of the values in the array. Suppose we have an array with numeric values that need to sum. To get the sum numeric values usually we use FOR loop and pass over the list. Some jobs can be done with a simple method, below is an example

Var numbers = [4, 5, 8, 9];
Var sum = numbers.reduce((previousValue,nextValue) => previousValue + nextValue);
Console.log(sum) // Out put 26

The reduce() method walks through the provided array.  It accepts a callback function as an argument. It starts calculation and processing from 0 to onward all indexes of the array. After calculation and processing provide the result of the reducer across all elements of the array in a single value.

Filter Unique Values

Sometimes we need unique values from specific data set to filter the theme. Let’s suppose we have an array of numbers included with some duplicate numbers and we need to sort out the unique values.

Example 1

var numericArray = [22,45,65,22,23,22];
const uniqueArray = [...new Set(numericArray )];
console.log(uniqueArray);

Example 2

In this example, we are using a filter as a prototype method on the array to remove the duplicates. The filter() method will execute on each index of the array and the custom method will check the values. If values match again, they will not include in the array. See below the example

function uniqueItems(value, index, self){
    return self.indexOf(value) === index
}

Then call the function as an argument inside the filter() method.

var unique = arry.filter(uniqueItems);
console.log(unique);

Both examples are providing unique values for the array.

How to Reduce the Length of the Array?

In many ways, we can reduce the length of the array, for example working with a response of services we need only the initial values of the array without needing the rest of the JSON. There is a faster solution than splice().

let data = [0, 22, 23, 24, 25, 75, 55, 65, 72, 25];
data.length = 4;
console.log(data); // Output: [0, 22, 23, 24];

It is the easiest way to reduce the length of an array in the case of initial values.

Shuffle Array Elements

The shortest way to shuffle array in JavaScript, or random data required from a specific dataset. For example, we have an array

const array = [1,2,3,4,5,6,7,8,9];

When we execute the function, we are required in a randomized position.

let list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list.sort(() => Math.random() - 0.5)
console.log(list)
// [object Array] (9)
[6,7,3,8,1,9,5,2,4]

So, getting a random array using this method is very simple and handy. In this method, you can get random data on every number of execution.

Conclusions

In this article, we have discussed the following items

  • Sum all of the values in the array, some time we have to push the values in an array and then calculate them
  • Filter the unique values of the array, we have discussed how to remove duplication and find out only unique values in the array.
  • Reduce the length of an array. Reducing array by length discussed a very simple tip to save development time.
  • Random Array: a simple way to use the random array.