From: Tuxedo on 13 Jan 2010 09:12 Hi, I have an array with image source and caption object pairs, such as: var library = [{ img: 'img01.jpg', caption: 'Caption 1'}, { img: 'img02.jpg', caption: 'Caption 2'}, { img: 'img03.jpg', caption: 'Caption 3'}]; I would like to return copies of the above as if the contents had been placed in two separate arrays, like: var photos = ['img01.jpg', 'img02.jpg', 'img03.jpg'] var captions = ['Caption 1', 'Caption 2', 'Caption 3'] But obviously I'd like to avoid repeating code unecessarily in form of duplicate typed out content as several arrays. Instead, I would like to access separate arrays of photos and captions, derived from the library array, as if they were two separate arrays in the first place, so that: ... alert(photos) would return: img01.jpg,img02.jpg,img03.jpg alert(captions) would return: Caption 1,Caption 2,Caption 3 How can the contents be copied from the 'library' array into two separate virtual arrays named 'photos' and 'captions' which can be accessed like normal single level arrays? Many thanks, Tuxedo
From: Scott Sauyet on 13 Jan 2010 09:49 On Jan 13, 9:12 am, Tuxedo <tux...(a)mailinator.com> wrote: > I have an array with image source and caption object pairs, such as: > > var library = [{ img: 'img01.jpg', caption: 'Caption 1'}, > { img: 'img02.jpg', caption: 'Caption 2'}, > { img: 'img03.jpg', caption: 'Caption 3'}]; > > I would like to return copies of the above as if the contents had been > placed in two separate arrays, like: > > var photos = ['img01.jpg', 'img02.jpg', 'img03.jpg'] > var captions = ['Caption 1', 'Caption 2', 'Caption 3'] var photos = [], captions = []; for (var i = 0, len = library.length; i < len; i++) { photos.push(library[i]["img"]); captions.push(library[i]["caption"]); } -- Scott
From: David Mark on 13 Jan 2010 10:53 On Jan 13, 10:13 am, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > David Mark wrote: > > Scott Sauyet wrote: > >> var photos = [], captions = []; > >> for (var i = 0, len = library.length; i < len; i++) { > >> photos.push(library[i]["img"]); > >> captions.push(library[i]["caption"]); > >> } > > > var photos = [], captions = []; > > photos.length = captions.length = library.length; > > for (var i = 0, len = library.length; i--;) { > > No :) No? Perhaps you meant you could improve on it? > > > photos[i] = library[i]["img"]; > > captions[i] = library[i]["caption"]; > > } > > var > photos = [], > captions = [], > len = library.length; > > photos.length = captions.length = len; > > for (var i = len; i--;) > { > var o = library[i]; > photos[i] = o.img; > captions[i] = o.caption; > } > Yes, the start's a bit nicer (library length). I don't know if that assignment to o will help though. You only saved two lookups. Not worth worrying about it at this point. I just didn't care for the original.
From: David Mark on 13 Jan 2010 10:54 On Jan 13, 10:17 am, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > Thomas 'PointedEars' Lahn wrote: > > var > > photos = [], > > captions = [], > > len = library.length; > > > photos.length = captions.length = len; > > > for (var i = len; i--;) > > { > > var o = library[i]; > > photos[i] = o.img; > > captions[i] = o.caption; > > } > > As we are iterating from end to start, does it even make sense to set the > `length' property? For it will be set in the first iteration anyway. > No. I originally had it going forward rather than reverse. Best to drop that opening bit entirely when going in reverse. I should have re-read it. Long night.
From: Thomas 'PointedEars' Lahn on 13 Jan 2010 11:34
David Mark wrote: > Thomas 'PointedEars' Lahn wrote: >> David Mark wrote: >> > Scott Sauyet wrote: >> >> var photos = [], captions = []; >> >> for (var i = 0, len = library.length; i < len; i++) { >> >> photos.push(library[i]["img"]); >> >> captions.push(library[i]["caption"]); >> >> } >> >> > var photos = [], captions = []; >> > photos.length = captions.length = library.length; >> > for (var i = 0, len = library.length; i--;) { ^^^^^ ^^^ >> No :) > > No? Perhaps you meant you could improve on it? If "to improve" means "let it do anything useful", then yes. Long night? ;-) >> >> > photos[i] = library[i]["img"]; >> > captions[i] = library[i]["caption"]; >> > } >> >> var >> photos = [], >> captions = [], >> len = library.length; >> >> photos.length = captions.length = len; >> >> for (var i = len; i--;) >> { >> var o = library[i]; >> photos[i] = o.img; >> captions[i] = o.caption; >> } > > Yes, the start's a bit nicer (library length). I don't know if that > assignment to o will help though. You only saved two lookups. Not > worth worrying about it at this point. Benchmarks suggest it would be about 20% faster in TraceMonkey 1.9.1.6. PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk> |