有人可以向我展示动态创建二维Javascript数组所需的JavaScript吗,如下所示?

所需的数组内容:

[["test1","test2","test3","test4","test5"],["test6","test7","test8","test9","test10"]]

来自alert(outterArray)的当前无效输出:
"test6","test7","test8","test9","test10","test6","test7","test8","test9","test10"

JavaScript代码:
var outterArray = new Array();
var innerArray = new Array();
var outterCount=0;
$something.each(function () {
   var innerCount = 0;//should reset the inner array and overwrite previous values?
   $something.somethingElse.each(function () {
        innerArray[innerCount] = $(this).text();
        innerCount++;
   }
   outterArray[outterCount] = innerArray;
   outterCount++;
}
alert(outterArray);

最佳答案

听起来您想将每个$something元素的文本数组映射到外部锯齿状数组中。如果是这样,请尝试以下操作

var outterArray = [];
$something.each(function () {
  var innerArray = [];
  $(this).somethingElse.each(function () {
     innerArray.push($(this).text());
  });
  outterArray.push(innerArray);
});

alert(outterArray);

09-11 17:41