Open In App

How to handle multiple numeric datasets for calculation using JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Let’s we have given a dataset consists of multiple numeric values, the task is to use that dataset for some calculations. So, we will be creating dictionary like data structure using array of objects. We will discuss how some JavaScript methods can be used to handle this type of data. To handle multiple numeric datasets for calculation we can use JavaScript filter() function and find() function.

Examples: Given a data of Employees.

 id |age |  sal  | exp | com
344 | 45 | 40000 | 12  |  3000
672 | 32 | 30000 | 5   |  8000

We have to follow a few steps to perform a calculation on multiple datasets.

Note: Array of object lets you store multiple values in a single variable. It stores a fixed-size sequential collection of elements of the same type. So, we are using an array of the dictionary-like object here.

Step 1: We will convert this dataset to an array of the object like below:

// Array of the dictionary object
employee = [
    {id: 344, age: 45, sal: 40000, exp: 12, com: 3000},
    {id: 672, age: 32, sal: 30000, exp: 5, com: 8000}
]

Step 2: Now we already have datasets in the form of an array of objects.

  • Example 1: Now we will see how we will manipulate or handle the data as per our need. In this example using filter() method (Can calculate multiple values.)




       
    <script>
      
        // Array of the dictionary object
        employee = [{
                id: 344,
                age: 45,
                sal: 40000,
                exp: 12,
                com: 3000
            },
            {
                id: 672,
                age: 32,
                sal: 30000,
                exp: 5,
                com: 8000
            }
        ]
      
    /* Method that calculates the total salary 
    of the employees according to the given
    experience*/
    function totalIncome(exp) {
      
        /* The filter() method uses another conditional
        method as an argument */
        let empWithExp = employee.filter((emp) => emp.exp === exp);
        let result = 0;
      
        /* All the employee's total salary with given experience is
        calculated here we are using forEach() method to calculate 
        salary of more than one employees */
        empWithExp.forEach((emp) => {
            result += emp.sal + emp.com;
        });
        return result;
    }
    //printing and calling the method totalIncome()
    document.write("Total income of Employee with experience "+
                " of 12years: " + totalIncome(12)); 
    </script>                    

    
    

  • Output:
    Total income of Employee with experience of 12years: 43000
  • Example 2: In this example using find() method (Used only to calculate values for unique data like id).




    <script>
      
        // Array of dictionary object
        employee = [{
                id: 344,
                age: 45,
                sal: 40000,
                exp: 12,
                com: 3000
            },
            {
                id: 672,
                age: 32,
                sal: 30000,
                exp: 5,
                com: 8000
            }
        ]
      
    // Function to calculate total salary of a
    // Employee using employee id
    function totalIncome(id) {
        let result;
      
        /* Find() method takes a conditional function 
           as a parameter, array.find() method in JavaScript 
           returns the value of the first element, In the array 
           that satisfies the provided testing function */
        let empWithId = employee.find((emp) => emp.id === id);
        result = empWithId.sal + empWithId.com;
        return result;
    }
      
    // Printing and calling the function totalIncome()
    document.write("Total income of the Employee having"
                      " id:672 = " + totalIncome(672)); 
      
    </script>                                    

    
    

  • Output:
    Total income of the Employee having id:672 = 38000


Last Updated : 17 Feb, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads