0

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]; 
} 
6
  • can you put the code you have already and expand on your explanation so its cleared for us to understand what you want to do. Commented Apr 5, 2016 at 22:44
  • $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]; } I only get the data like: $scope.filaCores[0][1]; when i try to push a object i cannot get the data. Everything that i try passing the object the console "says" undefined. Commented Apr 5, 2016 at 22:45
  • why not do $scope.filaCores[i] = $scope.object so you can do $scope.fileCores[0].name? Commented Apr 5, 2016 at 22:48
  • what is in fileCores?.. so for each of them you want to get the name and the second name into another array? Commented Apr 5, 2016 at 22:48
  • in fileCores is just my main array. every position of my main array i will nedd another array containing a object. i have 64 positions in my main array so i need to have a array of my objects inside a position of my main array. Commented Apr 5, 2016 at 22:52

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.