Is there an efficient way to calculate percent change for each key in an object?
The formula is (x/y-1)*100
- the x
being arry[0].key
and y
is arry[1].key
.
For example below is an array of objects that I would like to apply the formula above and return another object with percent changes.
var objArr = [{
"displayAd_imp": "3500",
"videoAd_imp": "1.5",
"tv_imp": "0.52"
}, {
"displayAd_imp": "1600",
"videoAd_imp": "2.55",
"tv_imp": "0.052"
}]
An example of the calculation would be
objArr[0].displayAd_imp / objArr[1].displayAd_imp - 1)*100 //this returns -3.74
and the data in the object would be {displayAd_imp_c : -3.74}
I'm not sure if looping both the object at the same time is a good idea, but I couldn't find a good way to loop through the keys and calculate the formula for each.
Hope I'm being clear on this, thanks for reading!