如何在嵌套数组中将字符串转换为数字?

时间:2016-08-03 06:13:09

标签: javascript arrays

我有以下数组,其中有一个数组 阵列。值在字符串中,我希望它是一个数字。什么是 在JavaScript中实现这一目标的最佳解决方案。

[
    {
        "key": "America",
        "values": [
            [
                "1469664000000",
                "1"
            ],
            [
                "1469750400000",
                "13"
            ],
            [
                "1469836800000",
                "2"
            ],
            [
                "1469923200000",
                "1"
            ],
            [
                "1470009600000",
                "1"
            ],
            [
                "1470096000000",
                "1"
            ]
        ]
    },
    {
        "key": "India",
        "values": [
            [
                "1469750400000",
                "1"
            ]
        ]
    },
    {
        "key": "China",
        "values": [
            [
                "1469664000000",
                "2"
            ],
            [
                "1469836800000",
                "1"
            ],
            [
                "1470096000000",
                "12"
            ]
        ]
    },
    {
        "key": "Africa",
        "values": [
            [
                "1470096000000",
                "1"
            ]
        ]
    },
    {
        "key": "UK",
        "values": [
            [
                "1469664000000",
                "3"
            ],
            [
                "1469750400000",
                "3"
            ],
            [
                "1469836800000",
                "1"
            ],
            [
                "1469923200000",
                "2"
            ],
            [
                "1470009600000",
                "4"
            ],
            [
                "1470096000000",
                "2"
            ]
        ]
    }
]

3 个答案:

答案 0 :(得分:2)

var data = [{"key":"America","values":[["1469664000000","1"],["1469750400000","13"],["1469836800000","2"],["1469923200000","1"],["1470009600000","1"],["1470096000000","1"]]},{"key":"India","values":[["1469750400000","1"]]},{"key":"China","values":[["1469664000000","2"],["1469836800000","1"],["1470096000000","12"]]},{"key":"Africa","values":[["1470096000000","1"]]},{"key":"UK","values":[["1469664000000","3"],["1469750400000","3"],["1469836800000","1"],["1469923200000","2"],["1470009600000","4"],["1470096000000","2"]]}];

data.forEach(function (o) {
  o.values.forEach(function (values) {
    values.forEach(function (value, i) {
      values[i] = parseInt(value, 10);
    });
  });
});

console.log(JSON.stringify(data, null, 4));

答案 1 :(得分:0)

var data = [{"key":"America","values":[["1469664000000","1"],["1469750400000","13"],["1469836800000","2"],["1469923200000","1"],["1470009600000","1"],["1470096000000","1"]]},{"key":"India","values":[["1469750400000","1"]]},{"key":"China","values":[["1469664000000","2"],["1469836800000","1"],["1470096000000","12"]]},{"key":"Africa","values":[["1470096000000","1"]]},{"key":"UK","values":[["1469664000000","3"],["1469750400000","3"],["1469836800000","1"],["1469923200000","2"],["1470009600000","4"],["1470096000000","2"]]}];

 data = data.map(function(t){
    var newVals = t.values.map(function(vs){
      return vs.map(function(v){
        return parseInt(v, 10);
     });
    });
    return {'key': t.key, values: newVals};
 });
 console.log(JSON.stringify(data));

答案 2 :(得分:-1)

您可以使用JSON.parse(您的json对象 或者可以使用两个forEachs