I am a beginner in JavaScript and am encountering an issue with rounding decimal numbers. I have an object with multiple key value pairs where the values can be strings, numbers, or null. My goal is to target the number key value pairs and round the number values to 2 decimal places. For example, if the value is 1.798
, I want to round it to 1.80
. Since I don't know how many key value pairs will be in my object, I just want to focus on the number values and update them.
I attempted using a forEach loop but later realized it works only on arrays.
var obj = {
name: 'abc',
term1: 0,
term2: 1.798,
term3: 1.9999,
term4: 0,
term5: null,
term6: 'xyz'
};
// Expected output
var obj = {
name: 'abc',
term1: 0,
term2: 1.80,
term3: 2.00,
term4: 0,
term5: null,
term6: 'xyz'
};