user3180997
user3180997

Reputation: 1926

Looping through object to perform maths to nearest high integer?

I am looking to loop through an object, get a set of value(s), and then work out the difference between that value and the next whole integer.

For example, let's say I have this object:

const data = [
   {
      "uid": "1121212",
      "amount": {
         "money": 11.2
      },
      "sourceAmount": {
         "money": 2.2
      }
   },
   {
      "uid": "343343443",
      "amount": {
         "money": 222.30
      },
      "sourceAmount": {
         "money": 444
      }
   },
   {
      "uid": "44",
      "amount": {
         "money": 54.21
      },
      "sourceAmount": {
         "money": 32
      }
   }
]

If possible, I am looking to loop through this and get the value's out of the amount.money keys, and work out the difference between that number and the next integer. So in the above example, it would be possible to get the values 11.22 & 222.30 & 54.21 and work out the difference to 12 223 and 55 as these are the next values. So the differences would be like: 78 & 70 and 79. These values (I was thinking) could be pushed into an array?

I have been looking through the map() feature, but am struggling to form something that does what I need? I have something that looks like:

const difference = [];

for (let i = 0, l = data.length; i < l; i++) {
    var money = data.amount.money[i];
    let roundedNumber = Math.ceil(money);
    let d = function (money, roundedNumber) { return Math.abs(roundedNumber - money); }
    difference.push(d)
}

I think this is a bad attempt, any help on best practise would be great.

The difference array should look like: const difference = ['78', '70', '79']; or const difference = ['0.78', '0.70', '0.79'];

Upvotes: 0

Views: 61

Answers (2)

norbitrial
norbitrial

Reputation: 15166

I think the following with .map() and Math.ceil() combination could work:

const data = [ { "uid": "1121212", "amount": { "money": 11.2 }, "sourceAmount": { "money": 2.2 } }, { "uid": "343343443", "amount": { "money": 222.30 }, "sourceAmount": { "money": 444 } }, { "uid": "44", "amount": { "money": 54.21 }, "sourceAmount": { "money": 32 } } ]
const result = data.map(e => (Math.ceil(e.amount.money) - e.amount.money).toFixed(2));
console.log(result);

The first value is 0.80 because of 12 - 11.2.

Please find as numbers parsed with Number.parseFloat():

const data = [ { "uid": "1121212", "amount": { "money": 11.2 }, "sourceAmount": { "money": 2.2 } }, { "uid": "343343443", "amount": { "money": 222.30 }, "sourceAmount": { "money": 444 } }, { "uid": "44", "amount": { "money": 54.21 }, "sourceAmount": { "money": 32 } } ]
const result = data.map(e => Number.parseFloat((Math.ceil(e.amount.money) - e.amount.money).toFixed(2)));
console.log(result);

I hope this helps!

Upvotes: 1

Carsten Massmann
Carsten Massmann

Reputation: 28196

If you want integers in your result then the following might help. Applying round() on float_val*100 is better than doing float_val.toFixed(2)*100, as the second method will lead to inaccurate results due to rounding errors.

const data = [ { "uid": "1121212", "amount": { "money": 11 }, "sourceAmount": { "money": 2.2 } }, { "uid": "343343443", "amount": { "money": 222.30 }, "sourceAmount": { "money": 444 } }, { "uid": "44", "amount": { "money": 54.216 }, "sourceAmount": { "money": 32 } } ]
const result = data.map(e =>Math.round(100*(1-e.amount.money%1))%100);
console.log(result);

Edit: Following your comment I added a further %100 to my formula. I also changed some of the sample values to demonstrate the rounding behaviour. The first value returns 0 now and the last (changed) value of 54.216 will lead to a remainder of 78.4 which will then be rounded to 78.

Upvotes: 0

Related Questions