-5

我有

var items = new Array('0,364,2113,0','0,0,0,0');

我想把它转换成多维数组,比如

var Finalitems = [[0, 364,2113,0], [0, 0, 0,0]];
4

4 回答 4

0

您可以只循环 items 数组,然后在,. 这将返回一个新数组,您可以将其添加到新的double dim array.

像这样的东西:

declare double dim array "dimarray";
declare int i = 0;
foreach(...in "items")
{
  var "newarray" = split(...);
  dimarray[i] = newarray;
  i++;
}
于 2012-09-29T12:23:04.993 回答
0

就像是

var items = new Array('0,364,2113,0','0,0,0,0');
var finalitems = [];

for (var i in items)
{
    vals = items[i].split(',');
    finalitems.push(vals);
}

?

于 2012-09-29T12:26:03.480 回答
0

要提供您所要求的,一个数字数组(不是字符串),请执行此操作。

var finalArray = items.map(function(item) {
                              return item.split(",").map(Number);
                           });
于 2012-09-29T12:54:10.750 回答
-2
<!DOCTYPE html>
<html>
<head>
<script>


function onBtnPress()
{
    var items = new Array('0,364,2113,0','0,0,0,0');
    var nItems, i;
    var result = new Array();

    nItems = items.length;
    for (i=0; i<nItems; i++)
    {
        curStr = items[i];
        curItemArray = curStr.split(',');
        len = curItemArray.length;
        for (index=0; index<len; index++)
        {
            curItemArray[index] = parseInt(curItemArray[index]);
        }

        result.push(curItemArray);
    }
    console.log(result);
}
</script>



<title></title>
</head>
<body>
    <input type='button' onclick='onBtnPress();' value='click me' >
</body>
</html>
于 2012-09-29T12:34:08.510 回答