Javascript中的多维数组

时间:2012-09-29 12:16:51

标签: javascript multidimensional-array

我有

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

我希望将其转换为多维数组,如

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

4 个答案:

答案 0 :(得分:1)

这样的东西
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);
}

答案 1 :(得分:0)

您可以循环使用items数组,并在,上拆分。这将返回一个新数组,您可以将其添加到新的double dim array

这样的事情:

declare double dim array "dimarray";
declare int i = 0;
foreach(...in "items")
{
  var "newarray" = split(...);
  dimarray[i] = newarray;
  i++;
}

答案 2 :(得分:0)

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

var finalArray = items.map(function(item) {
                              return item.split(",").map(Number);
                           });

答案 3 :(得分:-1)

<!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>