2010-07-26 58 views
1

我通過每個值試圖循環在我的字典裏,然後走出去,讓基於第一字典中的對象彼此字典通過每次循環迭代:迭代通過字典來添加到另一個

var dAlbumPhotos = {}; // holds a list of photo objects 
var dAlbumsAndPhotos = {}; // will hold all the album | dAlbumPhotos for each album 

for(var a in dAlbums) 
{ 
    dAlbumPhotos = GetPhotosForAlbum(userID, accessToken, a.id); 

    dAlbumsAndPhotos(a) = dAlbumPhotos; 
} 

我不太確定如何在沒有索引的情況下執行此操作。我需要通過dAlbumPhotos來增加,並追加到最終字典中,這是dAlbumPhotos

回答

3

它看起來像你真的用在Javascript關聯數組,所以你應該能夠改變你的代碼的最後一點是:

for(var a in dAlbums) 
{ 
    dAlbumPhotos = GetPhotosForAlbum(userID, accessToken, dAlbums[a].id); 
    dAlbumsAndPhotos[a] = dAlbumPhotos; 
} 
+0

但這是一個字典,雖然... – PositiveGuy 2010-07-26 19:58:50

+1

@coffeeaddict - 實際上,它是一個關聯數組(這是所有對象屬性都在Javascript中):http://www.quirksmode.org/js/associative.html。 – 2010-07-26 20:02:35

+1

謝謝...不知道這是一個關聯數組......認爲這個nvp是別的東西。 – PositiveGuy 2010-07-26 20:04:05

0

A for..in循環讓您回到索引,而不是循環中的對象。也就是說,a.id很可能無意義 - 你的意思是dAlbums[a].id。因此,dAlbumsAndPhotos[a]可能是您需要存儲結果的地方。

+0

我創建一個包含名稱值對...一個字典,以便dAlbumsAndPhotos將舉行專輯對象:dictionaryOfPhotoObjects – PositiveGuy 2010-07-26 20:00:15

+0

我只是不明白如何引用指數爲dAlbumsAndPhotos時試圖添加每張專輯和它的照片對字典 – PositiveGuy 2010-07-26 20:01:09

2

我覺得這是你追求的:

var dAlbumPhotos = {};  // holds a list of photo objects 
var dAlbumsAndPhotos = {}; // will hold all the album | dAlbumPhotos for each album 

for(var a in dAlbums) 
{ 
    dAlbumPhotos = GetPhotosForAlbum(userID, accessToken, dAlbums[a]); 

    dAlbumsAndPhotos[a] = dAlbumPhotos; 
} 
+0

好吧,是啊,現在第一個正確的道理。只是不明白dAlbumsAndPhotos的索引如何在這裏工作或被引用。 – PositiveGuy 2010-07-26 20:02:19

0

假設你有這樣一個JSON表中的照片數據:

var dPhotos = [{src: "/photos/a.jpg", album: "animals"}, {src: "/photos/b.jpg", album: "landscapes"}]; 

釷恩,你可以使用jOrder提取組索引是這樣的:

var table = jOrder(dPhotos) 
    .index('album', ['album'], {grouped: true}); 

var index = table.index('album').flat(); 

該指數將與在屬於該專輯dPhotos的一個行的ID列表相關的專輯名稱的字典。

jOrder可從http://github.com/danstocker/jorder