@rah-emil/js-helpers
Version:
jsHelpers - it is a JavaScript library that contains all the functions that we constantly write or google.
41 lines (33 loc) • 785 B
JavaScript
/**
* @module Obj
* @description Operations on objects
*/
/**
* We return the sum of the values of the array of objects
* @param key
* @returns {number} - sum
*/
export const sum = function(key){
let sum = 0;
Array.from(this).forEach(item => {
sum += parseFloat(item[key])
})
return sum
}
/**
* Returning the average of the fields
* @param key
* @param fractionDigits
* @returns {string}
*/
export const average = function(key, fractionDigits = 2){
let sum = this.sum(key)
return (sum / this.length || 1).toFixed(fractionDigits).replace(/\.0*$/, '')
}
/**
* Check object for emptiness
* @returns {boolean}
*/
export const isNotEmpty = function(){
return Object.keys(this).length !== 0
}