merging arrays

  • Thread starter Bush will disarm all workers next
  • Start date
B

Bush will disarm all workers next

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
 
M

Michael Winter

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
 
B

Bush will disarm all workers next

Michael Winter said:
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=[a, b];

and also
for(i=0; i < a.length; i++)
bg.xValues=[eval(a), eval(b)];

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

Dakshin
 
M

Michael Winter

On 17 Apr 2004 13:58:30 -0700, Bush will disarm all workers next

[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=[a, b];

and also
for(i=0; i < a.length; i++)
bg.xValues=[eval(a), eval(b)];


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
 
B

Bush will disarm all workers next

Michael Winter said:
On 17 Apr 2004 13:58:30 -0700, Bush will disarm all workers next

[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=[a, b];

and also
for(i=0; i < a.length; i++)
bg.xValues=[eval(a), eval(b)];


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top