Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   How to copy multi object array contents into single object arrays? (http://www.velocityreviews.com/forums/t940291-how-to-copy-multi-object-array-contents-into-single-object-arrays.html)

Tuxedo 01-13-2010 02:12 PM

How to copy multi object array contents into single object arrays?
 
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

Scott Sauyet 01-13-2010 02:49 PM

Re: How to copy multi object array contents into single objectarrays?
 
On Jan 13, 9:12*am, Tuxedo <tux...@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

David Mark 01-13-2010 02:59 PM

Re: How to copy multi object array contents into single object arrays?
 
Scott Sauyet wrote:
> On Jan 13, 9:12 am, Tuxedo <tux...@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"]);
> }
>


var photos = [], captions = [];
photos.length = captions.length = library.length;
for (var i = 0, len = library.length; i--;) {
photos[i] = library[i]["img"];
captions[i] = library[i]["caption"];
}




Thomas 'PointedEars' Lahn 01-13-2010 03:13 PM

Re: How to copy multi object array contents into single object arrays?
 
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 :)

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


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

Thomas 'PointedEars' Lahn 01-13-2010 03:17 PM

Re: How to copy multi object array contents into single object arrays?
 
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.


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

David Mark 01-13-2010 03:53 PM

Re: How to copy multi object array contents into single objectarrays?
 
On Jan 13, 10:13 am, Thomas 'PointedEars' Lahn <PointedE...@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.

David Mark 01-13-2010 03:54 PM

Re: How to copy multi object array contents into single objectarrays?
 
On Jan 13, 10:17 am, Thomas 'PointedEars' Lahn <PointedE...@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.

Thomas 'PointedEars' Lahn 01-13-2010 04:34 PM

Re: How to copy multi object array contents into single object arrays?
 
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@news.demon.co.uk>

Garrett Smith 01-13-2010 05:04 PM

Re: How to copy multi object array contents into single object arrays?
 
Thomas 'PointedEars' Lahn wrote:
> 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.
>

Setting the length will avoid step 10 in array [[Put]], so should be
fastest. IE versions may benefit even more from this.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/

David Mark 01-13-2010 05:10 PM

Re: How to copy multi object array contents into single object arrays?
 
Garrett Smith wrote:
> Thomas 'PointedEars' Lahn wrote:
>> 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? ;-)


Yes. Typo. Was supposed to be

for (var i = library.length; i--;)

.... and I didn't notice Thomas fixed that either. Bleary-eyed after
reading a bunch of (typically) horrible JS overnight. Why do the worst
programmers in the world write seemingly all of the "major" JS. It's
just not sustainable, so I think we'll all end up programming ES in
Flash or the like.

>>
>>>>> 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.
>>

> Setting the length will avoid step 10 in array [[Put]], so should be
> fastest. IE versions may benefit even more from this.


But, as Thomas noted, it will be set (for good) the first time as we are
going backwards.


All times are GMT. The time now is 01:04 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.