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

时间:2016-09-26 19:57:52

标签: javascript json

我有以下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 Consumption"”转换为“数字”;然后显示WEIGHTED_IVWAP_SLP& WEIGHTED_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位小数。如何仅转换特定字段?

3 个答案:

答案 0 :(得分:1)

这对你有用吗?好吧,这个答案不使用你的代码,但你应该能够应用它。这使用ifforfor ... in等语言结构,而不是Arraymap和{{3}等reduce的方法。 }}

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);

使用filter将值转换为数字,但请务必在决定更新值之前检查parseFloat结果。请使用NaN。不要认为你真的需要使用 ! IsNaN,但要把自己打倒。

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

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

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

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个小数位。而且他们是数字,而不是字符串。

答案 1 :(得分:1)

在启动reduce()之前,您可以通过在键上添加过滤器来稍微修改代码。其次,您可以在三元运算符中添加一个额外条件,以处理应该得到4个十进制数字的两个字段:

var parsedData = jsondataObj.map(function(obj) {
    return Object.keys(obj).filter (
        key => ["ORDER_QTY", "EXEC_QTY", "AVG_PX", "NOTIONAL_USD", "LIMIT_PX", 
                "ARRIVAL_MID_PX", "WEIGHTED_ARR_SLP", "WEIGHTED_IVWAP_SLP", 
                "LIQ_CONSUMPTION"].includes(key)
    ).reduce(function(memo, key) {
        var value = obj[key];
        memo[key] = !isNumeric(value) ? value
             : ["WEIGHTED_IVWAP_SLP", "WEIGHTED_ARR_SLP"].includes(key) 
                 ? Number(value).toFixed(4) 
                 : Number(value);
        return memo;
    }, Object.assign({}, obj))
});

请注意,您还必须将原始对象的副本作为reduce的初始值传递。为此,可以使用Object.assign

另外,请注意.toFixed将数字转回字符串类型,因为这是实际确保您看到4个十进制数字的唯一方法。如果您想真正拥有数字类型,请再次将Number()应用于该结果:

Number(Number(value).toFixed(4))

当然,在没有格式化的情况下输出这些数字时,不会显示尾随的十进制零。

var jsondata = `[{
    "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 jsondataObj = JSON.parse(jsondata);

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

var parsedData = jsondataObj.map(function(obj) {
    return Object.keys(obj).filter (
        key => ["ORDER_QTY", "EXEC_QTY", "AVG_PX", "NOTIONAL_USD", "LIMIT_PX", 
                "ARRIVAL_MID_PX", "WEIGHTED_ARR_SLP", "WEIGHTED_IVWAP_SLP", 
                "LIQ_CONSUMPTION"].includes(key)
    ).reduce(function(memo, key) {
        var value = obj[key];
        memo[key] = !isNumeric(value) ? value
             : ["WEIGHTED_IVWAP_SLP", "WEIGHTED_ARR_SLP"].includes(key) 
                 ? Number(value).toFixed(4) 
                 : Number(value);
        return memo;
    }, Object.assign({}, obj))
});

console.log(parsedData);

答案 2 :(得分:0)

这里有一个实际的例子:

var x = {
    a: '1.11111',
  b: '2.22222',
  c: '3.33333',
  d: '4.444444',
  e: '5.55555'
}

var y = ['a', 'd'];

for(key in x) {
    if(y.indexOf(key) != -1) {
    x[key] = Number(x[key]).toFixed(2);
  }
}

console.log(x);

X是您的对象,Y是一个数组,包含您要转换为固定的键列表,或者您想要执行的任何操作。 通过键时,如果键位于该数组中,则可以执行操作。

https://jsfiddle.net/dtuLxzy6/