473,566 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6796
On 17 Apr 2004 05:49:56 -0700, Bush will disarm all workers next
<da********@yah oo.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.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #2
Michael Winter <M.******@bluey onder.co.invali d> 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********@yah oo.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********@yah oo.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.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #4
Michael Winter <M.******@bluey onder.co.invali d> 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********@yah oo.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
6018
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 arrays from memory.Is there a command in VB5 that will do this ? Thanks, Tom M
3
1877
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, easiest, most efficient way of merging the 2 XML Documents? Can I use DataSet.Merge() facility in ADO.NET?? Any pre-requisites? Any other...
3
1771
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
3668
by: vidishasharma | last post by:
Can somebody suggest good URL which contains code for merging of 2 or more priority queues in c#.
11
4161
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 are to be found in stdlib.h • randomize() – use once to initialize the randomization process • rand() – generates a random number between 0 and the...
5
10905
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
1839
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, 14, 16} Dim intZ(17) As Long and need to merge array intX with array intY to form a new array intZ, is this possible?????
1
3093
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 a,b,c into another array f.. I can do two arrays, but I have issues when trying to do all three and when I do the even/odd compare I only get 2 numbers...
1
1647
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 having is this: Each House object contains an ArrayList of Storeys. Each Storey within the ArrayList contains a HashMap of Rooms. We are required to...
0
7666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.