gpt4 book ai didi

javascript - 将特定的json元素转换为json中的数字

转载 作者:行者123 更新时间:2023-12-02 14:09:51 24 4
gpt4 key购买 nike

我有以下 json 对象:

[{
"WEIGHTED_ARR_LAST_SLP": "0.03801186624130076",
"SLIPPAGE_INTERVAL_VWAP_BPS": "10.2711",
"ROOT_ORDER_ID": "735422197553491",
"ARRIVAL_MID_PX": "93.6100",
"WEIGHTED_ARR_SLP": "0.12323190317127024",
"AVG_PX": "93.6586",
"LEAVES_QTY": "0",
"WEIGHT": "0.02372627566400397",
"PARTICIPATION_RATE": "0E-12",
"LOCAL_REF_END_TIME": "2016-09-06 06:00:27.775",
"WEIGHTED_IVWAP_SLP": "0.2436949499725512",
"NOTIONAL_USD": "477940",
"LIQ_CONSUMPTION": "15.21",
"EXEC_QTY": "5103",
"CL_ORD_ID": "7245294057012908344",
"LOCAL_REF_START_TIME": "2016-09-06 05:59:57.844",
"SLIPPAGE_END_LAST_ARR_LAST_BPS": "1.6021",
"IVWAP_PX": "93.5625",
"LIMIT_PX": "93.6100",
"ORDER_ID": "735422197553491",
"SLIPPAGE_ARR_MID_BPS": "5.1939",
"ORDER_QTY": "5103",
"EXECUTION_STYLE": "2", {"ORDER_QTY": "5100", ..........}
}]

我只想转换“Ord Qty”、“Exec Qty”、“AVG Px”、“Notional”、“Limit Px”、“Arrival Px”、“Arrival Px Slpg”、“IVWAP Px Slpg” ,“LIQ 消耗” 为数字;然后显示 WEIGHTED_IVWAP_SLPWEIGHTED_ARR_SLP 为 4 位小数,而不是那么多。我试图通过以下方式做到这一点:

var jsondata = document.getElementById("jsonArray").value;
var jsondataObj = JSON.parse(jsondata);

function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}

var parsedData = jsondataObj.map(function(obj) {
return Object.keys(obj).reduce(function(memo, key) {
var value = obj[key];
//console.log(value);
memo[key] = isNumeric(value) ? Number(value).toFixed(4) : value;
//localStorage.setItem("storeditem", value);

return memo;
}, {})
});

但这正在转换所有具有 4 位小数点的数字字段。如何只转换特定字段?

最佳答案

这算是为你做你的工作吧?好吧,这个答案不使用您的代码,但您应该能够同样应用它。这使用诸如 if 之类的语言结构。 , forfor ... inArray 的方法相反像map , reduce ,和filter .

var a = [{
"WEIGHTED_ARR_LAST_SLP": "0.03801186624130076",
"SLIPPAGE_INTERVAL_VWAP_BPS": "10.2711",
"ROOT_ORDER_ID": "735422197553491",
"ARRIVAL_MID_PX": "93.6100",
"WEIGHTED_ARR_SLP": "0.12323190317127024",
"AVG_PX": "93.6586",
"LEAVES_QTY": "0",
"WEIGHT": "0.02372627566400397",
"PARTICIPATION_RATE": "0E-12",
"LOCAL_REF_END_TIME": "2016-09-06 06:00:27.775",
"WEIGHTED_IVWAP_SLP": "0.2436949499725512",
"NOTIONAL_USD": "477940",
"LIQ_CONSUMPTION": "15.21",
"EXEC_QTY": "5103",
"CL_ORD_ID": "7245294057012908344",
"LOCAL_REF_START_TIME": "2016-09-06 05:59:57.844",
"SLIPPAGE_END_LAST_ARR_LAST_BPS": "1.6021",
"IVWAP_PX": "93.5625",
"LIMIT_PX": "93.6100",
"ORDER_ID": "735422197553491",
"SLIPPAGE_ARR_MID_BPS": "5.1939",
"ORDER_QTY": "5103",
"EXECUTION_STYLE": "2"
}];
var fieldsToConvertToNumbers = [
"ORDER_QTY", "EXEC_QTY", "AVG_PX",
"NOTIONAL_USD", "LIMIT_PX", "ARRIVAL_MID_PX",
"WEIGHTED_IVWAP_SLP", "LIQ_CONSUMPTION"
];
for (var i = 0; i < a.length; ++i) {
var b = a[i];
for (var j = 0; j < fieldsToConvertToNumbers.length; ++j) {
var field = fieldsToConvertToNumbers[j];
if (field in b) {
var value = parseFloat(b[field]);
if (!isNaN(value))
b[field] = value;
}
}
}
var pre = document.createElement("pre");
pre.appendChild(document.createTextNode(JSON.stringify(a, null, ' ')));
document.body.appendChild(pre);

使用parseFloat将值转换为数字,但请务必检查 NaN决定更新值之前的结果。使用IsNaN为了那个原因。不要认为您真的需要使用 ! IsFinite ,但要让自己崩溃。

所以现在你想使用 toFixed修复字符串显示。

var text = (3.1415926535897932384626433832795028841971).toFixed(4);
var pre = document.createElement("pre");
pre.appendChild(document.createTextNode(text));
document.body.appendChild(pre);

现在您可以使用toFixed将数字转换为只有 4 位小数精度的字符串。但是,它们是字符串而不是数字。所以你必须使用 parseFloat 解析它们。 .

var a = [{
"WEIGHTED_ARR_LAST_SLP": "0.03801186624130076",
"SLIPPAGE_INTERVAL_VWAP_BPS": "10.2711",
"ROOT_ORDER_ID": "735422197553491",
"ARRIVAL_MID_PX": "93.6100",
"WEIGHTED_ARR_SLP": "0.12323190317127024",
"AVG_PX": "93.6586",
"LEAVES_QTY": "0",
"WEIGHT": "0.02372627566400397",
"PARTICIPATION_RATE": "0E-12",
"LOCAL_REF_END_TIME": "2016-09-06 06:00:27.775",
"WEIGHTED_IVWAP_SLP": "0.2436949499725512",
"NOTIONAL_USD": "477940",
"LIQ_CONSUMPTION": "15.21",
"EXEC_QTY": "5103",
"CL_ORD_ID": "7245294057012908344",
"LOCAL_REF_START_TIME": "2016-09-06 05:59:57.844",
"SLIPPAGE_END_LAST_ARR_LAST_BPS": "1.6021",
"IVWAP_PX": "93.5625",
"LIMIT_PX": "93.6100",
"ORDER_ID": "735422197553491",
"SLIPPAGE_ARR_MID_BPS": "5.1939",
"ORDER_QTY": "5103",
"EXECUTION_STYLE": "2"
}];
var fieldsToConvertToNumbers = [
"ORDER_QTY", "EXEC_QTY", "AVG_PX",
"NOTIONAL_USD", "LIMIT_PX", "ARRIVAL_MID_PX",
"LIQ_CONSUMPTION"
];
var fieldsToConvertToFixedNumbers = [
"WEIGHTED_IVWAP_SLP", "WEIGHTED_ARR_SLP"
];
for (var i = 0; i < a.length; ++i) {
var b = a[i];
for (var j = 0; j < fieldsToConvertToNumbers.length; ++j) {
var field = fieldsToConvertToNumbers[j];
if (field in b) {
var value = parseFloat(b[field]);
if (!isNaN(value))
b[field] = value;
}
}
for (var j = 0; j < fieldsToConvertToFixedNumbers.length; ++j) {
var field = fieldsToConvertToFixedNumbers[j];
if (field in b) {
var value = parseFloat(b[field]);
if (!isNaN(value))
b[field] = parseFloat(value.toFixed(4));
}
}
}
var pre = document.createElement("pre");
pre.appendChild(document.createTextNode(JSON.stringify(a, null, ' ')));
document.body.appendChild(pre);

所以现在只有特定字段有 4 位小数。它们是数字,而不是字符串。

关于javascript - 将特定的json元素转换为json中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39711343/

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