Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to copy multi object array contents into single object arrays?

Reply
Thread Tools

How to copy multi object array contents into single object arrays?

 
 
Tuxedo
Guest
Posts: n/a
 
      01-13-2010
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
 
Reply With Quote
 
 
 
 
Scott Sauyet
Guest
Posts: n/a
 
      01-13-2010
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
 
Reply With Quote
 
 
 
 
David Mark
Guest
Posts: n/a
 
      01-13-2010
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"];
}



 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-13-2010
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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-13-2010
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$> (2004)
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      01-13-2010
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.
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      01-13-2010
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.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-13-2010
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$>
 
Reply With Quote
 
Garrett Smith
Guest
Posts: n/a
 
      01-13-2010
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/
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      01-13-2010
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Ant COPY task, want to copy the directory itself, not just its contents! dxd@ptc.com Java 6 01-12-2011 12:30 PM
resolve single line with multiple items into mutliple lines, single items ela Perl Misc 12 04-06-2009 06:47 PM
Find Window from Client-Side (ActiveX?) to Copy Contents into Web =?Utf-8?B?R3JlZyBN?= ASP .Net 3 05-17-2007 12:04 PM
How do copy Strings from a single dimensional array to double dimensional array Venkat C++ 4 12-05-2003 09:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57