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

Last updated on March 10, 2023

In JavaScript, there are several ways to Sum, Filter and Shuffle the array values. But this article will cover some easiest of them.

Sum All Values of Array

During development in some cases, we 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 tasks 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 iterates through the provided array. It accepts a callback function as an argument and starts calculating 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.

Conclusion

This article demonstrated the following points,

  • 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.


Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts

Tags: JavaScript,