gpt4 book ai didi

javascript - 舍入 Json 对象的所有对象中的小数值

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

这是我的 JSON 字符串

[{"$id":"1","GeoName":"EAST","Value2":87.88221970554928,"Value4":90.71955219607294,"Value1":18.44377295716579,"Value3":16.732108234801206},{"$id":"2","GeoName":"WEST","Value2":87.88221970554928,"Value4":90.71955219607294,"Value1":18.44377295716579,"Value3":16.732108234801206}]"

这是我在 Stringyfying 之前的 JSONobject

**MyObj:**


0:Object
$id:"1"
Value1:18.44377295716579
GeoName:"EAST"
Value2:87.88221970554928
Value3:16.732108234801206
Value4:90.71955219607294
1:Object
$id:"2"
Value1:18.44377295716579
GeoName:"WEST"
Value2:87.88221970554928
Value3:16.732108234801206
Value4:90.71955219607294

有没有一种方法可以将我的 JSON 的每个对象中的所有值(或什至单独的值 1、值 2...)四舍五入到小数点后 2 位?

喜欢

0:Object
$id:"1"
Value1:18.44
GeoName:"EAST"
Value2:87.89
Value3:16.73
Value4:90.72
1:Object
$id:"2"
Value1:18.44
GeoName:"WEST"
Value2:87.89
Value3:16.73
Value4:90.71

我试过用

Obj = JSON.parse(JSON.stringify(MyObj, function (GeoName, Value1) {
return Value1.toFixed ? Number(Value1.toFixed(2)) : Value1;
}));

这对我不起作用。

有没有办法通过调整上面的代码行来实现这一点,或者是否有更好的方法来解决这个问题?

Edit1(在wostex建议的方法之后)

var numKeys = ['ShareValue','ReachValue','DepthValue','WidthValue'];
MyObj=Obj.map(e => Object.assign(e,Object.keys(e).filter(k => numKeys.includes(k)).map(v => Object.assign({[v]: parseFloat(e[v]).toFixed(2)}))))

这是我的对象的样子

0:Object
0:Object
ReachValue:87.88
1:Object
DepthValue:16.73
2:Object
ShareValue:18.44
3:Object
WidthValue:90.71
$id:"1"
ShareValue:18.44377295716579
GeoName:"EAST"
ReachValue:87.88221970554928
DepthValue:16.732108234801206
WidthValue:90.71955219607294

编辑2

Obj = JSON.parse(JSON.stringify(BubbleObj));
var numKeys = ['ShareValue','ReachValue','DepthValue','WidthValue'];
var MyObj=Obj.map(e => Object.assign(e,Object.keys(e).filter(k => numKeys.includes(k)).map(v => Object.assign({[v]: parseFloat(e[v]).toFixed(2)}))))

这里 Obj 有问题第一行的 JSON 字符串,BubbleObj 有我在问题中提到的 MyObj 对象。

最佳答案

最简单的方法就是使用 map - 只要您的 Value1-4 始终被调用即可

var input = [{"$id":"1","GeoName":"EAST","Value2":87.88221970554928,"Value4":90.71955219607294,"Value1":18.44377295716579,"Value3":16.732108234801206},{"$id":"2","GeoName":"WEST","Value2":87.88221970554928,"Value4":90.71955219607294,"Value1":18.44377295716579,"Value3":16.732108234801206}];

var output = input.map(function(e) {
return {
$id: e.$id,
GeoName: e.GeoName,
Value1: Math.round(e.Value1*100) /100,
Value2: Math.round(e.Value2*100) /100,
Value3: Math.round(e.Value3*100) /100,
Value4: Math.round(e.Value4*100) /100
}
});

console.log(output);

如果您不想对其进行硬编码,您可以寻找以 Value 开头的任何键 - 它有点复杂。

var input = [{"$id":"1","GeoName":"EAST","Value2":87.88221970554928,"Value4":90.71955219607294,"Value1":18.44377295716579,"Value3":16.732108234801206},{"$id":"2","GeoName":"WEST","Value2":87.88221970554928,"Value4":90.71955219607294,"Value1":18.44377295716579,"Value3":16.732108234801206}];

var output = input.map(function(e) {
return Object.keys(e).reduce(function(p,n){
if(n.startsWith("Value"))
p[n] = Math.round(e[n]*100)/100;
else
p[n] = e[n];
return p;
},{})
});

console.log(output);

关于javascript - 舍入 Json 对象的所有对象中的小数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44181348/

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