473,396 Members | 1,785 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,396 software developers and data experts.

combining 2 multi-dim arrays

I have 2 multi-dimensional array I want to combine and I'm drawing a
blank on the "how"

var array1 = [
["1","a"],
["2","b"]
];

var array2 = [
["3","c"],
["4","d"]
];

These methods do not work:

var array3 = [
[array1],
[array2]
];

var array3 = [
array1,array2
];

array3 should look like this:

["1","a"],
["2","b"],
["3","c"],
["4","d"]

Any help is appreciated.

Mike

Jan 12 '06 #1
3 1826
"mike" <hi****@charter.net> writes:
I have 2 multi-dimensional array I want to combine .... var array1 = [
["1","a"],
["2","b"]
];

var array2 = [
["3","c"],
["4","d"]
]; ..... array3 should look like this:

["1","a"],
["2","b"],
["3","c"],
["4","d"]


Try
var array3 = array1.concat(array2);

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 12 '06 #2
mike wrote:
I have 2 multi-dimensional array I want to combine and I'm drawing a
blank on the "how"

var array1 = [
["1","a"],
["2","b"]
];

var array2 = [
["3","c"],
["4","d"]
];

These methods do not work:

var array3 = [
[array1],
[array2]
];
That evaluates to

var array3 = [[[["1","a"], ["2","b"]]], [[["3","c"], ["4","d"]]]];
var array3 = [
array1,array2
];
That evaluates to:

var array3 = [[["1","a"], ["2","b"]], [["3","c"], ["4","d"]]];
array3 should look like this:

["1","a"],
["2","b"],
["3","c"],
["4","d"]

Any help is appreciated.


Specific:

var array3 = [array1[0], array[1], array2[0], array2[1]];

General:

var array3 = array1.concat(array2);

(JavaScript 1.2 (NN4+), JScript 3.0 (IE4+), ECMAScript 3) or

var
a = [array1, array2],
array3 = [];

for (var i = 0, len = a.length; i < len; i++)
{
var o = a[i];
for (var j = 0, len2 = o.length; j < len2; j++)
{
array3.push(o[j]); // or: array3[array3.length] = o[j];
}
}

The following method helps to recognize that:

Array.prototype.toString = function()
{
return "[" + this.join(",") + "]";
}

Examples:

window.alert([array1, array2, array3].join("\n"));
window.alert([array1, array2, array1.concat(array2)].join("\n"));
HTH

PointedEars
Jan 12 '06 #3
super ...

I like this:

var array3 = array1.concat(array2);

it works fine.

Mike

Jan 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: mikea_59 | last post by:
I am having trouble combining similar elements. Elements can be combined if they have the same name and the same attribute values. I can handle single level elements but am having problems with...
2
by: Mike Nau | last post by:
I've been trying to get a concrete answer to this for the last few days, but have come up short. I'm hoping someone here can help. Is there a way to take a set of c# modules (c# code compiled...
2
by: Chris Mullins | last post by:
I've spent a bit of time over the last year trying to implement RFC 3454 (Preparation of Internationalized Strings, aka 'StringPrep'). This RFC is also a dependency for RFC 3491...
3
by: alwayswinter | last post by:
I currently have a form where a user can enter results from a genetic test. I also have a pool of summaries that would correspond to different results that a user would enter into the form. I...
7
by: Barry | last post by:
Hi all, I've noticed a strange error on my website. When I print a capital letter P with a dot above, using & #7766; it appears correctly, but when I use P& #0775 it doesn't. The following...
5
by: M.Stanley | last post by:
Hi, I'm attempting to create a query that will combine 2 columns of numbers into one. The followng comes from 1 table with 4 fields (A,B,C,D) A B RESULT 700 000 700000 700 001 ...
3
by: Flip | last post by:
I'm looking at the O'Reilly Programming C# book and I have a question about extending and combining interfaces syntax. It just looks a bit odd to me, the two syntaxes look identical, but how does...
46
by: kyjabber | last post by:
I have a multi relationship database and I'm pulling the company's contact info, queryied if they are a grower, and a resulting list of their products by catgeory. I need to have the products listed...
4
by: Old Wolf | last post by:
As far as I can see, mbtowc and mbstowcs assume that there is exactly one wide character for each multi-byte sequence. How are you meant to cope with MBS that correspond to two wide characters? ...
5
by: Tristan Miller | last post by:
Greetings. Is it possible using HTML and CSS to represent a combining diacritical mark in a different style from the letter it modifies? For example, say I want to render Å‘ (Latin small letter...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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...
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
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...

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.