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?

 
 
Garrett Smith
Guest
Posts: n/a
 
      01-15-2010
Thomas 'PointedEars' Lahn wrote:
> Garrett Smith wrote:
>
>> var imgArray = library.map(filterByName("img"));
>> var captionArray = library.map(filterByName("caption"));
>> [...]
>>
>> Where not supported, Array.prototype.map functionality can be added, as
>> indicated on MDC page[1].

>
> What a waste. If an Array prototype method should be used here, then
> Array.prototype.forEach().
>


Array.prototype.forEach calls a supplied function on each element in the
Array. It does not create a new Array.

Array.prototype.map creates a new array with the results of calling a
supplied function on each element in the array.

And yes, much more efficient to just loop it yourself.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
 
Reply With Quote
 
 
 
 
Garrett Smith
Guest
Posts: n/a
 
      01-15-2010
Lasse wrote:
> On Jan 14, 7:17 pm, Scott Sauyet <(E-Mail Removed)> wrote:
>> In Firefox and Chrome it was more complicated. For array sizes of 10, 100, and
>> 1000 in Chrome, the backward was faster than forward by factors
>> approximately 1.5 - 2.5. But for 10000 elements, forward was faster
>> by a factor of about 1.25; I checked at 100000 elements too, and
>> forward was faster by a factor of about 1.5.

>
> The probable reason for this is that you start filling out the array
> from the back.
> This means that the array starts out with only one element af index
> 9999 - a very
> sparse array. Chrome and Firefox likely starts using an internal
> representation
> meant for sparse arrays, which is not as time efficient as linear
> arrays.


Did you notice the test code + results I posted?
Message ID: hioa6e$689$(E-Mail Removed)-september.org

> If you fill out the result array from the start, it will always be
> densely packed,
> and will stay that way when the backing store is increased.
>
> Try changing your array initialization to
> var len = library.length, photos = new Array(len), captions = new
> Array(len);


The Array constructor is a little bit slower from the solution I provided:
var len = library.length, photos = [], captions = [];
photos.length = captions.length = len;
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-15-2010
Garrett Smith wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Garrett Smith wrote:
>>> var imgArray = library.map(filterByName("img"));
>>> var captionArray = library.map(filterByName("caption"));
>>> [...]
>>>
>>> Where not supported, Array.prototype.map functionality can be added, as
>>> indicated on MDC page[1].

>>
>> What a waste. If an Array prototype method should be used here, then
>> Array.prototype.forEach().

>
> Array.prototype.forEach calls a supplied function on each element in the
> Array. It does not create a new Array.


You don't say.

> Array.prototype.map creates a new array with the results of calling a
> supplied function on each element in the array.


Exactly. If you use Array.prototype.map() you have to iterate *twice* (at
least internally) on the *same* array to get the two resulting arrays.
That's the waste.

var
imgArray = [],
captionArray = [];

library.forEach(function(e, i, a) {
imgArray[i] = e.img;
captionArray[i] = e.caption;
});


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      01-15-2010
On Jan 15, 7:41*pm, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
wrote:
>
> * var
> * * imgArray = [],
> * * captionArray = [];
>
> * library.forEach(function(e, i, a) {
> * * imgArray[i] = e.img;
> * * captionArray[i] = e.caption;
> * });


e is undefined...
--
Jorge.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-15-2010
Jorge wrote:

> Thomas 'PointedEars' Lahn wrote:
>> var
>> imgArray = [],
>> captionArray = [];
>>
>> library.forEach(function(e, i, a) {
>> imgArray[i] = e.img;
>> captionArray[i] = e.caption;
>> });

>
> e is undefined...


Where?


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      01-15-2010
On Jan 15, 8:00*pm, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
wrote:
> Jorge wrote:
> > Thomas 'PointedEars' Lahn wrote:
> >> var
> >> * imgArray = [],
> >> * captionArray = [];

>
> >> library.forEach(function(e, i, a) {
> >> * imgArray[i] = e.img;
> >> * captionArray[i] = e.caption;
> >> });

>
> > e is undefined...

>
> Where?


And 'a' is unused...
--
Jorge.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-15-2010
Jorge wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Jorge wrote:
>> > Thomas 'PointedEars' Lahn wrote:
>> >> var
>> >> imgArray = [],
>> >> captionArray = [];
>> >>
>> >> library.forEach(function(e, i, a) {
>> >> imgArray[i] = e.img;
>> >> captionArray[i] = e.caption;
>> >> });

>>
>> > e is undefined...

>>
>> Where?

>
> And 'a' is unused...


Yes, that argument does not need to be named here; however, the purpose
here was to show how forEach() could be used -- `a' is a reference to the
array.¹

You have not answered my question, though. I take it then that you found
out you were wrong.


PointedEars
___________
¹
<https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/forEach>
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      01-15-2010
On Jan 15, 8:27*pm, Jorge <(E-Mail Removed)> wrote:
>
> And 'a' is unused...


Sadly, it's not any faster...

http://jorgechamorro.com/cljs/093/
--
Jorge.
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      01-15-2010
On Jan 15, 8:39*pm, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
wrote:
> Jorge wrote:
>
> > And 'a' is unused...

>
> Yes, that argument does not need to be named here; however, the purpose
> here was to show how forEach() could be used -- `a' is a reference to the
> array.


And library is undefined...

> You have not answered my question, though. *I take it then that you found
> out you were wrong.


Who? Meee?
(It looked to me like ++another of your bugs.
--
Jorge.
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      01-15-2010
On Jan 15, 8:42*pm, Jorge <(E-Mail Removed)> wrote:
>
> Sadly, it's not any faster...
>
> http://jorgechamorro.com/cljs/093/


Except in FF3.6: http://tinyurl.com/ye94jhm
--
Jorge.
 
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