1

I have a NodeJS Express Code that is simply passing data from client to my back end server in request-response format. Now My client is expecting the dollar amounts in the JSON response as double datatype. How do I do it dynamically. For example below,

var data =   [
{
   "feeType":"Trip",
   "feeCategory":"Transaction",
   "feeAmount":0,
   "isApplicable":true,
   "isRequired":true,
   "applicableDeliveryType":"None",
   "appliedFeeAmount":0
},
{
   "feeType":"FerryFee",
   "feeCategory":"Passenger",
   "feeAmount":2,
   "isApplicable":true,
   "isRequired":false,
   "applicableDeliveryType":"None",
   "appliedFeeAmount":2
},
{
   "feeType":"WillCall",
   "feeCategory":"Service",
   "feeAmount":0,
   "isApplicable":false,
   "isRequired":false,
   "applicableDeliveryType":"Pickup",
   "appliedFeeAmount":0
},
{
   "feeType":"NotTravelling",
   "feeCategory":"Service",
   "feeAmount":0,
   "isApplicable":false,
   "isRequired":false,
   "applicableDeliveryType":"PrintAtHome",
   "appliedFeeAmount":0
},
{
   "feeType":"Pickup",
   "feeCategory":"Delivery",
   "feeAmount":0,
   "isApplicable":false,
   "isRequired":false,
   "applicableDeliveryType":"None",
   "appliedFeeAmount":0
},
{
   "feeType":"PrintAtHome",
   "feeCategory":"Delivery",
   "feeAmount":0,
   "isApplicable":false,
   "isRequired":false,
   "applicableDeliveryType":"None",
   "appliedFeeAmount":0
}

]

My Client is expecting as the number values with .0 or decimal numbers. How can I do that?

shiva nara
  • 43
  • 8
  • 1
    Possible duplicate of [How can I format numbers as currency string in JavaScript?](https://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-currency-string-in-javascript) – zod Aug 23 '19 at 20:11
  • Taki's answer is correct; just want to point out that your output is not JSON but an object, and you're not asking to change the data type, but the format. There is no "double" data type in JavaScript. – dikuw Aug 23 '19 at 20:17
  • JSON doesn't have a double data type, it only has a number type, and `36.0` and `36` are identically equivalent. – Quentin Aug 23 '19 at 21:40
  • What is your end goal here? There's no reason for whole numbers to be serialized as a decimal, that will just needlessly increase the transmission size when you send or receive the data. If you plan on displaying the data in some way you can simply call `toFixed()` on the number when inserting it into the view. – Jake Holzinger Aug 24 '19 at 00:14

1 Answers1

3

Use Object.entries to loop throgh the object and use .toFixed() to add the decimal :

const data = [{
    "feeType": "Trip",
    "feeCategory": "Transaction",
    "feeAmount": 0,
    "isApplicable": true,
    "isRequired": true,
    "applicableDeliveryType": "None",
    "appliedFeeAmount": 0
  },
  {
    "feeType": "FerryFee",
    "feeCategory": "Passenger",
    "feeAmount": 2,
    "isApplicable": true,
    "isRequired": false,
    "applicableDeliveryType": "None",
    "appliedFeeAmount": 2
  }
]

const result = data.map(o => Object.entries(o).reduce((acc, [key, value]) => {
  acc[key] = typeof value.toFixed === 'function' ? value.toFixed(1) : value;
  return acc;
}, {}))
console.log(result);

Or for .. in :

const data = [{
    "feeType": "Trip",
    "feeCategory": "Transaction",
    "feeAmount": 0,
    "isApplicable": true,
    "isRequired": true,
    "applicableDeliveryType": "None",
    "appliedFeeAmount": 0
  },
  {
    "feeType": "FerryFee",
    "feeCategory": "Passenger",
    "feeAmount": 2,
    "isApplicable": true,
    "isRequired": false,
    "applicableDeliveryType": "None",
    "appliedFeeAmount": 2
  }
]
const result = [];
data.forEach(o => {
  const tempObj = {};
  for (let key in o) {
    tempObj[key] = typeof o[key].toFixed === 'function' ? o[key].toFixed(1) : o[key];
  }
  result.push(tempObj);
});
console.log(result);
Taki
  • 17,320
  • 4
  • 26
  • 47
  • Your solution is good if the OP doesn't mind to have the values now as String rather Number. – Dez Aug 23 '19 at 20:25
  • 1
    @Dez In JS, you can't have a number with a `.0` at the end. Any numbers with zeros after the decimal will always be represented as integers. So it has to be a string. – IceMetalPunk Aug 23 '19 at 20:41
  • @taki What if I have a string value in the middle of the JSON Object? Does .toFixed convert everything to decimals? – shiva nara Aug 23 '19 at 20:43
  • No, if you have numbers as strings, you should parse them first, `parseFloat(value).toFixed(2)` and for the second example `parseFloat(data[key]).toFixed(2)` – Taki Aug 23 '19 at 20:47
  • "feeType":"FerryFee", "feeCategory":"Passenger", "feeAmount":2, "isApplicable":true, "isRequired":false, "applicableDeliveryType":"None", "appliedFeeAmount":2 } – shiva nara Aug 23 '19 at 21:31
  • I have something like that and need to convert the numbers to decimal and send back the same json to client – shiva nara Aug 23 '19 at 21:32
  • you should've mentionned that in the question, i edited the answer to check for strings `isNaN()` – Taki Aug 23 '19 at 21:38
  • @IceMetalPunk that's why the answer to the question should be: no, it is not possible right now in JavaScript. – Dez Aug 23 '19 at 22:14
  • @taki, Thanks for the update, but it gives me an error - toFixed is not a function – shiva nara Aug 23 '19 at 22:36
  • @taki - I have updated my original JSON input. Please help me in converting the numbers to decimal values. THanks – shiva nara Aug 23 '19 at 23:28
  • Updating you question gave me a better understanding of your needs, i updated my answer to add a check for `toFixed` function since it only exists on numbers – Taki Aug 23 '19 at 23:59
  • Worked like a charm! Thanks. May need your contact. your super hero :) – shiva nara Aug 24 '19 at 14:32