开发者

Iterating through a Dictionary to add to another

I am trying to loop through each value in my dictionary and then go out and get another dictionary based on one of the objects in the first dictionary through each loop iteration:

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

for(var a in d开发者_C百科Albums)
{
    dAlbumPhotos = GetPhotosForAlbum(userID, accessToken, a.id);

    dAlbumsAndPhotos(a) = dAlbumPhotos;
}

I'm not quite sure how to do this without an index. I need to increment through the dAlbumPhotos and append to the final dictionary that album and it's dAlbumPhotos


It looks like you're really using an Associative Array in Javascript so you should be able to change the last bit of your code to be:

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


I think this is what you're after:

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


A for..in loop gives you back the indexes, not the objects in the loop. That is, a.id is likely nonsensical -- you really mean dAlbums[a].id. It follows that dAlbumsAndPhotos[a] is probably where you need to store the results.


Suppose you have the photo data in a json table like this:

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

Then you can use jOrder to extract the group index like this:

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

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

This index will hold a dictionary with album names associated with the a list of ids of rows in dPhotos belonging to that album.

jOrder is available from http://github.com/danstocker/jorder

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜