从JavaScript中的其他数组填充特定数组索引
我有两个数组,从JavaScript中的其他数组填充特定数组索引,javascript,arrays,Javascript,Arrays,我有两个数组,arr1和arr2。它们都是二维的。我想将某些数组值从arr1复制到arr2 例如,我想将值从arr1[9][9]复制到arr2[0][0]。我的猜测是写arr2[0][0]=arr1[9][9]但是失败了 我在这个网站上看到了一些类似的问题,但他们没有回答我的问题 以下是针对特定情况的代码。代码是由谷歌应用程序脚本编写的 // eplList and attList are both arrays. They are filled below (I checked, the
arr1
和arr2
。它们都是二维的。我想将某些数组值从arr1
复制到arr2
例如,我想将值从arr1[9][9]
复制到arr2[0][0]
。我的猜测是写arr2[0][0]=arr1[9][9]代码>但是失败了
我在这个网站上看到了一些类似的问题,但他们没有回答我的问题
以下是针对特定情况的代码。代码是由谷歌应用程序脚本编写的
// eplList and attList are both arrays. They are filled below (I checked, the values exist)
var eplList = epl.getRange(2, 2, eplLastRow, 2).getValues();
var attList = attsheet.getRange(3, 1, attLastRow, 20).getValues();
var eplListLength = eplList.filter(String).length;
var attListLength = attList.filter(String).length;
// Declaring the empty array I want to fill
var masterArray = [];
var ix, jx, day;
// Here I begin to fill the array
for (ix = 0; ix < eplListLength; ix++)
{
masterArray[ix][0] = eplList[ix][0]; // This is where I am getting the error message
masterArray[ix][1] = eplList[ix][1];
for (jx = 0; jx < attListLength; jx++)
{
if (eplList[ix][0] == attList[jx][day*4-4])
masterArray[ix][6+day] = masterArray[ix][6+day].concat(" ", attList[jx][day*4-2], ": ",attList[jx][day*4-3]);
};
// and some morecode
};
//eplList和attList都是数组。它们填写在下面(我选中了,值存在)
var eplList=epl.getRange(2,2,eplLastRow,2).getValues();
var attList=attsheet.getRange(3,1,attLastRow,20).getValues();
var eplListLength=eplList.filter(String).length;
var attListLength=attList.filter(String).length;
//声明要填充的空数组
var masterArray=[];
var ix,jx,日;
//在这里,我开始填充数组
对于(ix=0;ix
我收到的错误是“TypeError:无法将未定义的属性“0”设置为“”,若要解决特定问题,请尝试此代码,但我不确定下面的代码):
masterArray[ix]=[eplList[ix][0],eplList[ix][1];//这就是我收到错误消息的地方
共享您的阵列数据。共享您的阵列以及您得到的错误更新问题。谢谢您需要初始化内部数组:masterArray[ix]=[]
for
循环中的。此外,变量day
从未设置为value@user256872哪里您需要初始化每个内部数组for(ix=0;ix
所以,您根本不需要这一行:masterArray[ix][1]=eplList[ix][1];
没问题,下面的代码(嵌套的)怎么样。失败了吗?到目前为止还不错,它还在开发中:)@user256872您需要在某个地方设置day
的值。它没有定义,6+day
将返回NaN
是的,我在代码中设置了它,我只是不小心将它删除了。谢谢