2016-04-05 28 views
0

64 konumlu bir ana dizim var, her konum için başka bir diziye ihtiyacım var. benzeri: someArray[index] = [someObject].Diziyi başka bir dizide nasıl ittirebilirim?

Bu dizileri nasıl oluşturabilirim? ve someObject.name , someObject.lastName'u nasıl edinebilirim? İşte

$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

zaten yazdığınız kodu koyabilir ve açıklamanızı genişletebilir; yap. –

+0

'$ scope.filaCores = []; \t $ scope.object = {} \t $ scope.object.name = "name"; \t $ scope.object.secondname = "secondname"; için \t \t (var i = 0; i <10; i ++) { \t \t $ scope.filaCores [i] = [$ scope.object.name, $ scope.object.secondname]; \t} Yalnızca şu verileri elde ediyorum: $ scope.filaCores [0] [1]; Bir nesneyi itmeye çalıştığımda veri alamıyorum. Nesneyi geçmeyi denediğim her şey konsolu belirsiz "diyor". –

+0

neden "$ scope.filaCores [i] = $ scope.object" yapamazsınız, böylece $ scope.fileCores [0] .name' yapabilirsin? – david

cevap

1

gitmek: https://jsfiddle.net/jsg81gg8/1/

senin tarifine dayanarak ben diziler dizisi için gerek görmüyorum İşte

kodudur. Ama burada istediğin şey buydu. Alt dizi için kaç dizi öğeyi belirtmediniz, böylece üç ile bir örnek göstereceğim. Eğer sadece toplam 64 yapıya ihtiyaç duyarsanız, iç diziye ihtiyaç yoktur. Gerçekten alt dizi gerekmez ve yalnızca o zaman bu gibi görünmek basitleştirilmiş olabilir 64 yapılarını gerekiyorsa

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

: https://jsfiddle.net/Ldafbwbk/1/

GÜNCELLEME: Ve burada üçte biri güncelleştirilmiş sorunuzu daha yakından anlatan örnek: https://jsfiddle.net/7st8fnw5/3/

+0

kesinlikle benim için çalıştı, çok teşekkürler! –

İlgili konular