JSFiddle - React, Tailwind, and code Playground
by apasaja
JavaScript
/* function sum( obj ) {
let sum = 0;
for( let el in obj ) {
if( obj.hasOwnProperty( el ) ) {
if(typeof obj[el] == 'number') {
sum += parseFloat( obj[el] );
}
}
}
return sum;
}
let sample = [
{ a: "1" , b: 2 , c:3 },
{ a: "1" , b: 2 , c:3 }
]
let sample2 =
{ a: "1" , b: 2 , c:3 }
let summed = sum( sample2 );
for(let key in sample) {
Object.assign(sample[key], {d: sum(sample)})
}
Object.assign(sample2, {d: summed})
//console.log( summed )
console.log(sample2)
*/
function modify(arr) {
for (const item of arr) {
sum(item)
}
}
function sum(obj) {
let result = 0
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] == "number" && obj[key]<500) {
result += obj[key]
}
}
}
obj.sum = result
}
const sample = [
{a: 1573747200000, b: 2, c: 3, d: 4},
{a: 1573833600000, b: 2, c: 3, d: 4},
]
modify(sample)
console.log(sample)