By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,816 Members | 2,053 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,816 IT Pros & Developers. It's quick & easy.

combining 2 multi-dim arrays

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
"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

P: n/a
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

P: n/a
super ...

I like this:

var array3 = array1.concat(array2);

it works fine.

Mike

Jan 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.