How to sum the values of a JavaScript object?
You could put it all in one function:
function sum( obj ) { var sum = 0; for( var el in obj ) { if( obj.hasOwnProperty( el ) ) { sum += parseFloat( obj[el] ); } } return sum;} var sample = { a: 1 , b: 2 , c:3 };var summed = sum( sample );console.log( "sum: "+summed );
How to sum the array of object values and assigned them to the relevant key name
solved using reduce and forEach
Inside the reduce function I'm running a forEach
on the array of keys of the incomes
object/attribute. For each key which is a date I'm checking if the accumulator of the reduce function contains an attribute for each date and creates if not. After creating the attribute I'm summing the value for the current date attribute.
const data = [{
group: 'A',
incomes: {
"2019-12": 100,
"2020-12": 200,
"2021-12": 15
}
},
{
group: 'B',
incomes: {
"2019-12": 25,
"2020-12": 50,
}
}
]
const totalIncomes = data.reduce((acc, curr) => {
Object.keys(curr.incomes).forEach((key, index) => {
if (!acc[key]) {
acc[key] = 0
}
acc[key] += curr.incomes[key]
})
return acc
}, {})
console.log(totalIncomes)
Sum values of a JavaScript object
If numOfElemements meant elements in data object, then current implementation will do. Else uncomment the existing commented lines, and comment the line after the last commented line. Which considers elements in the data object.
const obj = {
1: {
data: {
a: 100,
z: 100,
e: 50,
},
},
2: {
data: {
a: 10,
z: 10,
e: 20,
},
},
};
function groupBySum(obj) {
let data = {};
let percentage = {};
// let numberOfElements = 0;
for (let i in obj) {
// numberOfElements += 1;
for (let j in obj[i].data) {
if (data[j] === undefined) {
data[j] = obj[i].data[j];
} else {
data[j] += obj[i].data[j];
}
}
}
for (let i in data) {
// percentage[i] = data[i] / numberOfElements;
percentage[i] = data[i] / Object.keys(data).length;
}
return {
data,
percentage,
};
}
console.log(groupBySum(obj));
sum all values of an object in an array of objects javascript
Use Array.reduce to calculate the sum
const sum = array.reduce((acc, o) => acc + parseInt(o.value), 0)
How to sum object values by a key value that is an date object?
Kind of ES6 way and grouping by hash solution:
const arr = [{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":100,"Fehler":5},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":5,"Fehler":1},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":6,"Fehler":65},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"222","Menge":10,"Fehler":5},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"444","Menge":29,"Fehler":1},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":1,"Fehler":1}]
const result = Object.values(arr.reduce((acc, obj) => {
const [Datum] = obj.Datum.date.split(' ');
const Menge = (acc[Datum]?.Menge + obj.Menge) || obj.Menge;
const Fehler = (acc[Datum]?.Fehler + obj.Fehler) || obj.Fehler;
const Ratio = Fehler / Menge;
acc[Datum] = { Datum, Menge, Fehler, Ratio };
return acc;
}, {}));
console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
Related Topics
How to Detect When a Tab Is Focused or Not in Chrome with JavaScript
Do Websockets Allow for P2P (Browser to Browser) Communication
Using the Haversine Formula in JavaScript
How Do Cors and Access-Control-Allow-Headers Work
How to Auto-Reload Files in Node.Js
Js/Es6: Destructuring of Undefined
JavaScript Roundoff Number to Nearest 0.5
Dynamic Require in Requirejs, Getting "Module Name Has Not Been Loaded Yet for Context" Error
Validating User's Utf-8 Name in JavaScript
What's the API Key for in Google Maps API V3
How Does Setinterval and Settimeout Work
How to Access Nested JSON Data
How to Send Authorization Header with Axios
Cross Domain Localstorage with JavaScript
Npm Start Error with Create-React-App
Scroll Smoothly to Specific Element on Page
What Do Double Brackets Mean in JavaScript and How to Access Them