简体   繁体   中英

How can i push a array in another array?

i have a main array with 64 positions, for each position i need another array. like: someArray[index] = [someObject] .

How can i create those arrays ? and How can i get someObject.name , someObject.lastName ?

Here is the code:

$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]; 
} 

Here you go: https://jsfiddle.net/jsg81gg8/1/

Based on your description I do not see the need for an array of arrays. But since that is what you asked for here you go. You didn't specify how many array elements for the sub array so I'm going to show an example with three. If you only need 64 total structures then the inner array is not needed.

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);

If you really don't need the sub array, and you only need 64 structures then it could be simplified to look like this: https://jsfiddle.net/Ldafbwbk/1/

UPDATE: And here is a third example that more closely resembles your updated question: https://jsfiddle.net/7st8fnw5/3/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM