tags:

views:

38

answers:

4

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 dAlbums)
{
    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

A: 

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.

Cory Petosky
I'm creating a dictionary that contains name value pairs...so dAlbumsAndPhotos will hold an album object : dictionaryOfPhotoObjects
CoffeeAddict
I just did not understand how to reference the index for the dAlbumsAndPhotos when trying to add each album and its dictionary of photo pairs
CoffeeAddict
+1  A: 

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;
}
Dave Ward
ok, yea that first correctly makes sense now. Just don't understand how the index of dAlbumsAndPhotos is working here or being referenced.
CoffeeAddict
+1  A: 

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;
}
Justin Niessner
but this is a dictionary though...
CoffeeAddict
@coffeeaddict - Actually, it's an associative array (which is what all object properties are in Javascript): http://www.quirksmode.org/js/associative.html.
Justin Niessner
thanks...did not know this was an associative array...thought that this nvp was something else.
CoffeeAddict
ok so by doing this dAlbumsAndPhotos[a] = dAlbumPhotos; there is really no "index" to speak of interms of needing to refernce an index for the associative array in order to add each a and dAlbumPhotos pair?
CoffeeAddict
so all a "dictionary" is in JS is an object with properties....and properties of course are the name/value pairs
CoffeeAddict
@coffeeaddict - In this case, a is going to be the index (you can use the same for loop structure to loop through dAlbumsAndPhotos as you did through dAlbums).
Justin Niessner
crap, so I do really need a dictionary even though I'm not storing a string in the name for each name/value pair. Because in the end I want a list of name/value pairs stuffed into a return object where it's customObject/array as the name value pairs. I need sleep badly.
CoffeeAddict
Ok, I'm wondering if I'm just not even looking at this right. How do you create collection or dictionary of object / object pairs?
CoffeeAddict
A: 

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

Dan Stocker