我有一个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];
}
答案 0 :(得分:1)
您可以: 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/