我有一个主要的数组以 64 的位置,为我需要另一个数组的每个位置。如︰ someArray[index] = [someObject]
。
如何创建这些阵列?怎样才能和 someObject.name , someObject.lastName
?
下面是代码︰
$scope.filaCores = [];
$scope.object = {}
$scope.object.name = "name";
$scope.object.secondname= "secondname";
for(var i = 0; i <10; i++) {
$scope.filaCores[i] = [$scope.object.name, $scope.object.secondname];
}
在这里你去︰ https://jsfiddle.net/jsg81gg8/1/
根据你的描述我看不需要数组的数组。但既然这是你在这里的问你去。 您没有指定多少数组元素的子数组,所以我要展示与三个例子。 如果您只需要 64 总结构则不需要内部数组。
window.getRandomInt = function() {
return Math.floor(Math.random() * (10000 - 1 + 1)) + 1;
}
mainArray = []; /* you said you wanted 64 of these */
subArray = []; /* you didn't specify how many of these, the example below will assume 3 per each mainarray */
for (i = 0; i < 64; i++) {
for (j = 0; j < 3; j++) {
/* I'm using getRandomInt() just so you can see all the names are different */
subArray[j] = {name:"John"+getRandomInt(), lastname:"Doe"+getRandomInt()};
}
mainArray[i] = subArray;
}
/* Press F12 and go to the console tab, run this script, and you will see the output of the entire array */
console.log(mainArray);
/* You can access a specific element like this... */
alert('Alerting mainArray[23][2].lastname: '+mainArray[23][2].lastname);
如果你真的不需要子数组中,然后你只需要 64 结构可以简化它,看起来像这样︰ https://jsfiddle.net/Ldafbwbk/1/
更新︰这里有更多相似你更新的问题第三个例子︰ https://jsfiddle.net/7st8fnw5/3/