gpt4 book ai didi

javascript - json数据值类型

转载 作者:行者123 更新时间:2023-11-30 17:09:34 24 4
gpt4 key购买 nike

我想将 json 数组值从字符串更改为来自 php 脚本的浮点值。都是字符串类型。我想要它作为浮点类型。

[{"year":"2008","value":"4169.20"},
{"year":"2009","value":"4067.50"},
{"year":"2010","value":"4848.40"},
{"year":"2011","value":"5654.80"},
{"year":"2012","value":"6071.50"}]

但我希望它看起来像。可能吗?

[{"year":"2008","value":4169.20},
{"year":"2009","value":4067.50},
{"year":"2010","value":4848.40},
{"year":"2011","value":5654.80},
{"year":"2012","value":6071.50}]

最佳答案

最简单的方法是在将数据编码为 JSON 之前将 value 转换为 float。

但是如果你只想在 JS 中工作:

var data = [ /* your data */ ];

data.forEach(function(item){
item.value = +item.value;
});

输出:

[
{"year":"2008","value":4169.2},
{"year":"2009","value":4067.5},
{"year":"2010","value":4848.4},
{"year":"2011","value":5654.8},
{"year":"2012","value":6071.5}
]

关于javascript - json数据值类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27293482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com