473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 6791
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tmenke | last post by:
Can someone help, I have two arrays and want to merge them into a third array.What I want to do is, after I create this third array (combination of the first two) is to erase the original two...
3
by: Patrick | last post by:
I have got 2 XML documents, both of which conform to the same XSD Schema, which define possible optional elements. The 2 XML documents contain 2 disjoint set of XML elements. What is the best,...
3
by: André Hänsel | last post by:
Hi! Is there a better way to do this? $command = '$array = array_merge($array,$array);'; eval($command); Regards, André
8
by: vidishasharma | last post by:
Can somebody suggest good URL which contains code for merging of 2 or more priority queues in c#.
11
by: holla | last post by:
Write the following functions that can be called by a C program: Write a function to populate an array with random integers between 0 and 99. Use the following functions of which the prototypes...
5
by: John | last post by:
Hi Is there a way to merge two or more single dimension string arrays into a single, single dimension string array? Thanks Regards
5
by: Sarge | last post by:
Good Afternoon All, I am working with Visual Studio.Net 2003 If I have Two 1D Arrays: Dim intX() As Integer = {1, 3, 5, 7, 9, 11, 13, 15, 17} Dim intY() As Integer = {2, 4, 6, 8, 9, 10, 12,...
1
by: chiefychf | last post by:
I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge...
1
by: kliopatraisis | last post by:
I think I've been working on this assignment for too long and my brain has stopped making connections! Basically, we are making a very simple version of AutoCAD, called HomeCAD. The problem I am...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.