我从不同来源提取不同的信息。这些信息是动态的。我想将它全部整合到一个多维数组中。我一直很好,直到我遇到试图将动态信息推入多维数组的问题。
此时我甚至不知道谷歌要找到我需要的解决方案。下面是代码的简化示例。亮点是:
示例代码:
InformationByTent = [
{tentNum:1,Occupant01:"John",Occupant02:"Sam",Occupant03:"Harry",Friday:"Build Fire",Saturday:"Hike Preparation",Sunday:"Cook Dinner"},
{tentNum:2,Occupant01:"Dawn",Occupant02:"Amy",Occupant03:"Jane",Friday:"Hike Preparation",Saturday:"Sentry",Sunday:"Build Fire"},
{tentNum:3,Occupant01:"Nate",Occupant02:"Peter",Friday:"Cook Dinner",Saturday:"Cook Dinner",Sunday:"Sentry"},]
通缉结果:
Occupant01:tent01Occupants[0],
Occupant02:tent01Occupants[1],
Occupant03:tent01Occupants[2]
我不知道如何定义标识符或编写语法/方法来推动它:
return
..当我不知道每个阵列中将有多少人时。任何指针都会有所帮助!
答案 0 :(得分:1)
做什么:
var info;
for (i = 0; i < totalNumberTents; i++) {
// Assume there are lines of code here that randomizes the duties and assigns three
// of those to the FridayDuty, SaturdayDuty, SundayDuty variables. Results:
// FridayDuty = "Build Fire";
// SaturdayDuty = "Hike Preparation";
// SundayDuty = "Cook Dinner";
numberOfOccupants = Tent01Occupants.length;
info = {
tentNum: i + 1,
Friday: FridayDuty,
Saturday: SaturdayDuty,
Sunday: SundayDuty,
};
for (var x = 0; x < Tent01Occupants.length; x++) {
info["Occupant" + (x + 1)] = Tent010Occupants[x];
}
// And repeat the same for other variables
InformationByTent.push(info);
}