我有
var items = new Array('0,364,2113,0','0,0,0,0');
我想把它转换成多维数组,比如
var Finalitems = [[0, 364,2113,0], [0, 0, 0,0]];
我有
var items = new Array('0,364,2113,0','0,0,0,0');
我想把它转换成多维数组,比如
var Finalitems = [[0, 364,2113,0], [0, 0, 0,0]];
您可以只循环 items 数组,然后在,
. 这将返回一个新数组,您可以将其添加到新的double dim array
.
像这样的东西:
declare double dim array "dimarray";
declare int i = 0;
foreach(...in "items")
{
var "newarray" = split(...);
dimarray[i] = newarray;
i++;
}
就像是
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);
}
?
要提供您所要求的,一个数字数组(不是字符串),请执行此操作。
var finalArray = items.map(function(item) {
return item.split(",").map(Number);
});
<!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>