HighDots Forums  

Re: merging arrays

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: merging arrays in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Michael Winter
 
Posts: n/a

Default Re: merging arrays - 04-17-2004 , 09:08 AM






On 17 Apr 2004 05:49:56 -0700, Bush will disarm all workers next
<dakshing64 (AT) yahoo (DOT) com> wrote:

Quote:
I'm new to JavaScript. So please excuse my mistakes.
I need help on merging two arrays into a third one.
There is an array a = {188, 180, 159, 67 }
There is another array b={'Rio de Janeiro', 'Sao Paulo', 'Brasilia',
'Belo Horizonte'}
I assume you meant

a = [ 188, 180, 159, 67 ];
b = [ 'Rio de Janeiro', 'Sao Paulo', 'Brasilia', 'Belo Horizonte' ];

Braces ({}) denote object literals, brackets ([]) denote array literals.

Quote:
How can I merge them to produce an array that is equivalent to this:
bg.xValues[0] = [188,'Rio de Janeiro'];
bg.xValues[1] = [180,'Sao Paulo'];
bg.xValues[2] = [159,'Brasilia'];
bg.xValues[3] = [67 ,'Belo Horizonte'];
function mergeArrays( x, y ) {
var t = [], n = Math.min( x.length, y.length );

for( var i = 0; i < n; ++i ) {
t[ i ] = [ x[ i ], y[ i ]];
}
return t;
}

bg.xValues = mergeArrays( a, b );

If you can guarantee that "a" and "b" will always have the same length,
you can reduce the assignment to "n" to

n = x.length; // or y.length

Hope that helps,
Mike

--
Michael Winter
M.Winter (AT) blueyonder (DOT) co.invalid (replace ".invalid" with ".uk" to reply)


Reply With Quote
  #2  
Old   
Michael Winter
 
Posts: n/a

Default Re: merging arrays - 04-17-2004 , 05:26 PM






On 17 Apr 2004 13:58:30 -0700, Bush will disarm all workers next
<dakshing64 (AT) yahoo (DOT) com> wrote:

[snip]

Quote:
Thanks for the reply. I tried the code you've posted without success I
used
for(i=0; i < a.length; i++)
bg.xValues[i]=[a[i], b[i]];

and also
for(i=0; i < a.length; i++)
bg.xValues[i]=[eval(a[i]), eval(b[i])];
Using eval() certainly won't do any good (there are very few reasons to
*ever* use eval()).

Quote:
The bg object is a strange beast that expects atomic values like 145 or
'test'.
Any thoughts?
Unless you can point to some documentation that explains what the bg
object is, I haven't got a clue.

Sorry,
Mike

--
Michael Winter
M.Winter (AT) blueyonder (DOT) co.invalid (replace ".invalid" with ".uk" to reply)


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.