0

我有一个带有数字的表,但它们存储为字符串,所以我尝试使用函数 parseFloat 将其转换为整数表。但是,无论我如何执行 for 循环,它都会给我一个空白表。我可以解析该行,它会给我一个 int。我也可以解析单个 int,但似乎无法解析行或表格。这是我到目前为止所拥有的。这只是试图转换一行。我为整个表尝试了两个 for 循环,但也没有用。谢谢。

 var c =[];
    var entries = $.parseJSON('<?php print(json_encode($try, true)); ?>');  
       for (var j = 0; j < 12; j++)
       {
         for (var i = 0; i < 7; i++)
         { 
         c [j][i] = parseFloat(entries[j][i]);
         }
       }
alert(c);

这里是条目 json 编码 [["-248","-163","-455","-1413","-1294","-1296","-1089"],["-172"," -219","-1186","-1368","-1480","-1079","-845"],["-98","-198","-703","-996" ,"-1100","-585","-616"],["-116","-241","-498","-642","-704","-354","- 430"],["-137","-117","-264","-525","-533","-269","-476"],["-12","87" ,"-257","-463","-551","-302","-535"],["170","61","-250","-472","-659" “-220”,"-605"],["159","96","-234","-513","-617","-196","-710"],["185","117" ,"-272","-521","-610","-258","-798"],["208","95","-234","-534","-696" ,"-280","-854"],["192","151","-188","-641","-739","-279","-957"],["249 ","223","-235","-684","-763","-339","-978"]]-798"],["208","95","-234","-534","-696","-280","-854"],["192","151"," -188","-641","-739","-279","-957"],["249","223","-235","-684","-763"," -339","-978"]]-798"],["208","95","-234","-534","-696","-280","-854"],["192","151"," -188","-641","-739","-279","-957"],["249","223","-235","-684","-763"," -339","-978"]]

4

2 回答 2

3

您必须将数组的第二维初始化为数组。就目前而言,c[j]它只是一个值,因此您无法c[j][i]对其进行操作。在您的第一个循环中也有一个错误,for您需要比较 的值j,而不是i。请参阅此固定代码:

var c = [];
var entries = $.parseJSON('<?php print(json_encode($try, true)); ?>');  
   for (var j = 0; j < 12; j++)
   {
     c[j] = [];
     for (var i = 0; i < 7; i++)
     { 
         c [j][i] = parseFloat(entries[j][i]);
     }
   }
alert(c);
于 2013-03-02T02:26:04.530 回答
0

在您的第一个循环for (var j = 0; i < 12; j++)中,您正在使用ifor 迭代,但它是未定义的,它应该j代替i

var c =[];
var entries = $.parseJSON('<?php print(json_encode($try, true)); ?>');  
for (var j = 0; j < entries.length ; j++)
{
    c[j] = [];
    for (var i = 0; i < entries[i].length ; i++)
    {          
       c [j][i] = parseFloat(entries[j][i]);
    }
}
于 2013-03-02T02:26:44.653 回答