469,347 Members | 7,368 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,347 developers. It's quick & easy.

merging arrays

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

Thanks
Dakshin
Jul 23 '05 #1
4 6629
On 17 Apr 2004 05:49:56 -0700, Bush will disarm all workers next
<da********@yahoo.com> wrote:
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.
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.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #2
Michael Winter <M.******@blueyonder.co.invalid> wrote in message news:<op**************@news-text.blueyonder.co.uk>...
On 17 Apr 2004 05:49:56 -0700, Bush will disarm all workers next
<da********@yahoo.com> wrote:
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.
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

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])];

The bg object is a strange beast that expects atomic values like 145 or 'test'.
Any thoughts?

Dakshin
Jul 23 '05 #3
On 17 Apr 2004 13:58:30 -0700, Bush will disarm all workers next
<da********@yahoo.com> wrote:

[snip]
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()).
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.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #4
Michael Winter <M.******@blueyonder.co.invalid> wrote in message news:<op**************@news-text.blueyonder.co.uk>...
On 17 Apr 2004 13:58:30 -0700, Bush will disarm all workers next
<da********@yahoo.com> wrote:

[snip]
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()).
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


Here is the link that has Javascript that uses bg at the bottom of the
page. I've the Graph.js downloaded on another machine. I'll post the
details soon.
http://www.codeproject.com/jscript/dhtml_graph.asp
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Tmenke | last post: by
3 posts views Thread by Patrick | last post: by
3 posts views Thread by André Hänsel | last post: by
8 posts views Thread by vidishasharma | last post: by
5 posts views Thread by John | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.