Javascript Data Type How to - Round some values in an object array








Question

We would like to know how to round some values in an object array.

Answer


<!DOCTYPE html>
<html>
<head>
</head><!--   w ww  .j  av  a2 s  . c om-->
<body>
  <script type='text/javascript'>
var array = [ 
    {name:1, price:3.99, tax:0.20}, 
    {name:2, price:1.40, tax:0.15},
    {name:3, price:0.99, tax:0.10}, 
];
array.forEach(function ( elem ) {
    elem.price = Math.round( elem.price );
});
document.write( array[0].price );

</script>
</body>
</html>

The code above is rendered as follows: