Convert json string value to number

后端 未结 4 1301
孤城傲影
孤城傲影 2021-01-03 03:24

I have a JSON string that reads:

[{
    \"id\": \"id2\",
    \"index\": \"2\",
    \"str\": \"str2\",
    \"cent\": \"200\",
    \"triplet\": \"222\"
},
{
           


        
4条回答
  •  粉色の甜心
    2021-01-03 03:57

    Try this

    // Iterate thorugh the array
    [].forEach.call(x, function(inst, i){
        // Iterate through all the keys
        [].forEach.call(Object.keys(inst), function(y){
            // Check if string is Numerical string
            if(!isNaN(x[i][y]))
                //Convert to numerical value
                x[i][y] = +x[i][y];
        });
    
    });
    
    console.log(x);
    

    Live FIddle

提交回复
热议问题