admin管理员组

文章数量:1360003

I have the following JSON, I want add mas in between the numbers, but when ever I do that the JSON fails. My workign version of the code can be seen in the fiddle link below

FIDDLE I want to change this 11488897 to 11,488,897

Is this possible to do? How can this be done?

thanks for your help

[{
    "name": "",
    "data": ["Totals","Total1 ","Total 2","total 3" ]
}, {
    "name": "Amount1",
    "data": [48353330,38079817,37130929,1957317]
}, {
    "name": "Amount2",
    "data": [11488897,8902674,8814629,497369]
}]

I have the following JSON, I want add mas in between the numbers, but when ever I do that the JSON fails. My workign version of the code can be seen in the fiddle link below

FIDDLE I want to change this 11488897 to 11,488,897

Is this possible to do? How can this be done?

thanks for your help

[{
    "name": "",
    "data": ["Totals","Total1 ","Total 2","total 3" ]
}, {
    "name": "Amount1",
    "data": [48353330,38079817,37130929,1957317]
}, {
    "name": "Amount2",
    "data": [11488897,8902674,8814629,497369]
}]
Share Improve this question edited Mar 22, 2016 at 16:03 user244394 asked Mar 22, 2016 at 15:50 user244394user244394 13.5k25 gold badges84 silver badges142 bronze badges 3
  • 1 Don't you mean 11,488,897, or do you have a specific number formatting scheme you're trying to implement? – Rory McCrossan Commented Mar 22, 2016 at 15:54
  • 1 Why change the underlying data? Hold an int (without mas) and then format the output of this int to include mas, when you output it. – Liam Commented Mar 22, 2016 at 15:54
  • @RoryMcCrossan You are right i want the json to have it in this format 11,488,897 instead of 11488897 , currently when i add ma by escape it .. doesnt seem to work – user244394 Commented Mar 22, 2016 at 16:00
Add a ment  | 

3 Answers 3

Reset to default 4

If you want to preserve mas, you just need to use strings:

"data": ["48,353,330","38,079,817","37,130,929","1,957,317"]

Whether that's a good idea or not is another story. Typically you'd want your JSON returned by the server to include raw (i.e., integer) data, and then just format it however you want when you're actually using it. That way the same RPC endpoint can be used to fetch data for use in a chart or for any other purpose that might arise later on.

try this:

var data = [{
    "name": "",
    "data": ["Totals","Total1 ","Total 2","total 3" ]
}, {
    "name": "Amount1",
    "data": [48353330,38079817,37130929,1957317]
}, {
    "name": "Amount2",
    "data": [11488897,8902674,8814629,497369]
}];

data.forEach(function(obj) {
  obj.data = obj.data.map(function(item) {
    if (typeof item == 'number') {
      return item.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    } else {
      return item;
    }
  });
});
alert(JSON.stringify(data, true, 4));

I don't know if it's cross-browser but if you do this

var number = 11488897;
number = number.toLocaleString('en');

You'll get the number (string) with mas on decimals

本文标签: javascriptHow to escape commas in JSON for read by highchartStack Overflow