stepheniok
stepheniok

Reputation: 405

Javascript - Calculations with properties of an object

I am currently attempting to calculate the growth percentage change of the properties in an object. I have the calculation that I am able to successfully do with an array of integers, but I am having a hard time applying the same logic to the properties of an object.

My expected outcome is to perform a calculation to data similar to this: year2011-year2010 /year2010; year2012-year2011 /year2011 etc.. in an array such as this:

calculationData = [14.9274 , -0.1875, 0.6122]

Here is an example of a manual way of solving this:

let calc2010 = data.year2011 - data.year2010 / data.year2010
let calc2011 = data.year2011 - data.year2012 / data.year2011

calculationData.push(calc2010, calc2011)

const data = [{'year2010': 4323, 'year2011': 64532, 'year2012': 52432, 'year2013': 84532, 'year2014': 63232, 'year2015': 49332 }]

How can I accomplish this?

In the code snippet below; I provided two examples First, is the properties of objects I am attempting to calculate, and second is showing the calculations working for an array of integers

const data = [{'year2010': 4323, 'year2011': 64532, 'year2012': 52432, 'year2013': 84532, 'year2014': 63232, 'year2015': 49332 }]


let calculationData = data.map(function (stPer, i) {
        return 100 * (stPer - data[i - 1]) / (data[i - 1]);
    });

console.log(calculationData)


const dataArr = [ [312], [4342], [4343] ]

    let calculationDataTwo = dataArr.map(function (stPer, i) {
        return 100 * (stPer - dataArr[i - 1]) / (dataArr[i - 1]);
    });
    
    
    
    console.log(calculationDataTwo) 

Upvotes: 0

Views: 84

Answers (2)

user6713871
user6713871

Reputation:

Here is a solution which converts your object into an array of data in ascending order by year.

const data = {'year2010': 4323, 'year2011': 64532, 'year2012': 52432, 'year2013': 84532, 'year2014': 63232, 'year2015': 49332 };


const calculationData = Object.entries(data).sort(([year1], [year2]) => {
    return Number(year1.slice(4)) - Number(year2.slice(4));
}).map(([year, value]) => value);

This is using the Object.entries function to create an array which looks like: [['year2010', 4323], ['year2011', 64532], etc...]

Next, we can sort it and map it easily. Sorting may not be needed if you guarantee the data is pre-sorted, in which case you only need to use map.

The result is an array like this: [4323, 64532, 52432, 84532, 63232, 49332]

You should be able to plug that into your existing code.

EDIT: I see there is still some additional processing to do after you get to this point. Do you need any help after this point?

Upvotes: 1

Barmar
Barmar

Reputation: 781096

You need to put each year in a different array element, not all years in the same object.

And when you use map, you need to start with array index 1, because there's no previous element before index 0 to compare with. You can do this by mapping over a slice.

const data = [{
    year: 2010,
    value: 4323
  },
  {
    year: 2011,
    value: 64532
  },
  {
    year: 2012,
    value: 52432
  },
  {
    year: 2013,
    value: 84532
  },
  {
    year: 2014,
    value: 63232
  }, 
  {
    year: 2015,
    value: 49332
  }
]


let calculationData = data.slice(1).map(function(stPer, i) {
  return 100 * (stPer.value - data[i].value) / data[i].value;
});

console.log(calculationData)

Upvotes: 0

Related Questions