<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Javascript Array Merge: array_merge</title>
	<atom:link href="http://stereointeractive.com/blog/2007/07/19/javascript-array-merge-array_merge/feed/" rel="self" type="application/rss+xml" />
	<link>http://stereointeractive.com/blog/2007/07/19/javascript-array-merge-array_merge/</link>
	<description>Development Blog</description>
	<lastBuildDate>Thu, 17 Jun 2010 09:37:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Andrew Noyes</title>
		<link>http://stereointeractive.com/blog/2007/07/19/javascript-array-merge-array_merge/comment-page-1/#comment-413</link>
		<dc:creator>Andrew Noyes</dc:creator>
		<pubDate>Mon, 27 Apr 2009 08:01:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stereodevelopment.com/2007/07/19/javascript-array-merge-array_merge/#comment-413</guid>
		<description>Here&#039;s my take on array_merge for JavaScript:

http://gist.github.com/102384

For better or for worse, it allows collections to be merged into arrays. It checks the type of the objects strongly to make sure they&#039;re compatible. It works fine in Firefox and Safari that I know of.</description>
		<content:encoded><![CDATA[<p>Here&#8217;s my take on array_merge for JavaScript:</p>
<p><a href="http://gist.github.com/102384" rel="nofollow">http://gist.github.com/102384</a></p>
<p>For better or for worse, it allows collections to be merged into arrays. It checks the type of the objects strongly to make sure they&#8217;re compatible. It works fine in Firefox and Safari that I know of.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Noyes</title>
		<link>http://stereointeractive.com/blog/2007/07/19/javascript-array-merge-array_merge/comment-page-1/#comment-412</link>
		<dc:creator>Andrew Noyes</dc:creator>
		<pubDate>Mon, 27 Apr 2009 02:34:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stereodevelopment.com/2007/07/19/javascript-array-merge-array_merge/#comment-412</guid>
		<description>I like how Subhasis Deb&#039;s function differentiates between Arrays and Objects, since Objects functionally simulate PHP associative arrays, and more often than not are what I use for handling data. However, there are a few things I would recommend changing.  First of all, the for...in loop pattern is incredibly inefficient with arrays, and I also never recommend using it for readability purposes. I switch between JavaScript and PHP all the time, and in PHP, we have the notation foreach ($var as $arr) to loop through the array, where $var is the value of the current iteration. However, in JavaScript, the for...in notation simply initializes an index. This isn&#039;t a big deal if you work with JavaScript all the time, but I&#039;ve been tripped up by more than one loop where I was expecting the val in for (var val in myArr) to be equal to one of myArr&#039;s values, rather than an interator. Secondly, rather than using a loop, you can use concat, which is a native method and nearly infinitely faster than using a JavaScript loop. 

There is one major bug in this function if you&#039;re scripting in a multi-frame DOM environment. Objects in different windows do not share their constructor with the objects in the window that spawned them, so the evaluation [].constructor == Array is actually false in child windows. Since this check is false, the next check asks if the arguments are an instance of Object, which is always true since everything is an Object in JavaScript.(i.e. [] instanceof Object evaluates true). The solution to this is to call the constructor in the context of the global Object object using the apply method:

Object.prototype.toString.apply([]);    // == &quot;[object Array]&quot;

This also leaves out array-like values, which are very important in DOM scripting, since document.getElementsByTagName doesn&#039;t return an Array, it returns a NodeList, which is not an instanceof Array. Because of these array-like objects, Arrays are part of a broader group of objects called Collections. For this, I picked up this handy check from the YUI source code to check if an element is a collection (Arrays included):
&lt;pre lang=&quot;javascript&quot;&gt;
function isValidCollection(obj) {
    try {
        return (
            obj &amp;&amp;    // Element exists
            typeof obj != &quot;string&quot; &amp;&amp;    // Weeds out strings for length test
            obj.length &amp;&amp;
            !obj.tagName &amp;&amp;    // Isn&#039;t an HTML node
            !obj.alert &amp;&amp;    // not window
            typeof obj[0] != &quot;undefined&quot;     // Has at least one element
        );
    } catch (e) {
        return false;
    }
}
&lt;/pre&gt;

I might rewrite this function to be sensitive to array-like objects and be a little more compatible with multi-frame environments.</description>
		<content:encoded><![CDATA[<p>I like how Subhasis Deb&#8217;s function differentiates between Arrays and Objects, since Objects functionally simulate PHP associative arrays, and more often than not are what I use for handling data. However, there are a few things I would recommend changing.  First of all, the for&#8230;in loop pattern is incredibly inefficient with arrays, and I also never recommend using it for readability purposes. I switch between JavaScript and PHP all the time, and in PHP, we have the notation foreach ($var as $arr) to loop through the array, where $var is the value of the current iteration. However, in JavaScript, the for&#8230;in notation simply initializes an index. This isn&#8217;t a big deal if you work with JavaScript all the time, but I&#8217;ve been tripped up by more than one loop where I was expecting the val in for (var val in myArr) to be equal to one of myArr&#8217;s values, rather than an interator. Secondly, rather than using a loop, you can use concat, which is a native method and nearly infinitely faster than using a JavaScript loop. </p>
<p>There is one major bug in this function if you&#8217;re scripting in a multi-frame DOM environment. Objects in different windows do not share their constructor with the objects in the window that spawned them, so the evaluation [].constructor == Array is actually false in child windows. Since this check is false, the next check asks if the arguments are an instance of Object, which is always true since everything is an Object in JavaScript.(i.e. [] instanceof Object evaluates true). The solution to this is to call the constructor in the context of the global Object object using the apply method:</p>
<p>Object.prototype.toString.apply([]);    // == &#8220;[object Array]&#8221;</p>
<p>This also leaves out array-like values, which are very important in DOM scripting, since document.getElementsByTagName doesn&#8217;t return an Array, it returns a NodeList, which is not an instanceof Array. Because of these array-like objects, Arrays are part of a broader group of objects called Collections. For this, I picked up this handy check from the YUI source code to check if an element is a collection (Arrays included):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">function</span> isValidCollection<span class="br0">&#40;</span>obj<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">try</span> <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="br0">&#40;</span>
            obj <span class="sy0">&amp;&amp;</span>    <span class="co1">// Element exists</span>
            <span class="kw1">typeof</span> obj <span class="sy0">!=</span> <span class="st0">&quot;string&quot;</span> <span class="sy0">&amp;&amp;</span>    <span class="co1">// Weeds out strings for length test</span>
            obj.<span class="me1">length</span> <span class="sy0">&amp;&amp;</span>
            <span class="sy0">!</span>obj.<span class="me1">tagName</span> <span class="sy0">&amp;&amp;</span>    <span class="co1">// Isn't an HTML node</span>
            <span class="sy0">!</span>obj.<span class="kw3">alert</span> <span class="sy0">&amp;&amp;</span>    <span class="co1">// not window</span>
            <span class="kw1">typeof</span> obj<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">!=</span> <span class="st0">&quot;undefined&quot;</span>     <span class="co1">// Has at least one element</span>
        <span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span> <span class="kw1">catch</span> <span class="br0">&#40;</span>e<span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="kw2">false</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>I might rewrite this function to be sensitive to array-like objects and be a little more compatible with multi-frame environments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gregory</title>
		<link>http://stereointeractive.com/blog/2007/07/19/javascript-array-merge-array_merge/comment-page-1/#comment-354</link>
		<dc:creator>gregory</dc:creator>
		<pubDate>Fri, 26 Dec 2008 05:05:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stereodevelopment.com/2007/07/19/javascript-array-merge-array_merge/#comment-354</guid>
		<description>I have a prototype that handles more complex merging of arrays as well as inserting arrays into other arrays all without needing a for / while loops. Read more about it here: http://gregorytomlinson.com/encoded/2008/12/25/javascript-array-merge/</description>
		<content:encoded><![CDATA[<p>I have a prototype that handles more complex merging of arrays as well as inserting arrays into other arrays all without needing a for / while loops. Read more about it here: <a href="http://gregorytomlinson.com/encoded/2008/12/25/javascript-array-merge/" rel="nofollow">http://gregorytomlinson.com/encoded/2008/12/25/javascript-array-merge/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Meves</title>
		<link>http://stereointeractive.com/blog/2007/07/19/javascript-array-merge-array_merge/comment-page-1/#comment-337</link>
		<dc:creator>Scott Meves</dc:creator>
		<pubDate>Wed, 26 Nov 2008 18:21:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stereodevelopment.com/2007/07/19/javascript-array-merge-array_merge/#comment-337</guid>
		<description>@Subhasis Deb:  Thanks! Sorry our comment code formatting is so messed up.</description>
		<content:encoded><![CDATA[<p>@Subhasis Deb:  Thanks! Sorry our comment code formatting is so messed up.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Subhasis Deb</title>
		<link>http://stereointeractive.com/blog/2007/07/19/javascript-array-merge-array_merge/comment-page-1/#comment-335</link>
		<dc:creator>Subhasis Deb</dc:creator>
		<pubDate>Tue, 25 Nov 2008 08:17:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stereodevelopment.com/2007/07/19/javascript-array-merge-array_merge/#comment-335</guid>
		<description>Following is my version of independent array_merge (compact):
&lt;pre lang=&quot;javascript&quot;&gt;
/***
* Simulate php array_merge function
*
... [more] * @param {Object/Array} arr1
* @param {Object/Array} arr2
* var a1 = {&#039;aa&#039;:100, &#039;bb&#039;:2, &#039;cc&#039;:[6,7], &#039;dd&#039;:[12,13], &#039;ee&#039;:{&#039;15&#039;:15,&#039;16&#039;:16}};
* var b1 = {&#039;xx&#039;:101, &#039;bb&#039;:5, &#039;cc&#039;:8, &#039;dd&#039;:[14,15], &#039;ee&#039;:{&#039;17&#039;:17,&#039;18&#039;:18}};
* var c = array_merge(a1, b1);
* console.log(c) [in firebug]
* Output: {&#039;aa&#039;:100, &#039;bb&#039;: 5, &#039;cc&#039;:[6,7], &#039;dd&#039;:[12,13,14,15], &#039;ee&#039;:{&#039;15&#039;:15,&#039;16&#039;:16,&#039;17&#039;:17,&#039;18&#039;:18}, &#039;xx&#039;:101}
*/
var array_merge = function(arr1, arr2){
  if((arr1 &amp;&amp; (arr1 instanceof Array)) &amp;&amp; (arr2 &amp;&amp; (arr2 instanceof Array))){
    for (var idx in arr2) {
      arr1.push(arr2[idx]);
    }
  }else if((arr1 &amp;&amp; (arr1 instanceof Object)) &amp;&amp; (arr2 &amp;&amp; (arr2 instanceof Object))){
    for(var idx in arr2){
      if(idx in arr1){
        if (typeof arr1[idx] == &#039;object&#039; &amp;&amp; typeof arr2 == &#039;object&#039;) {
          arr1[idx] = array_merge(arr1[idx], arr2[idx]);
        }else{
          arr1[idx] = arr2[idx];
        }
      }else{
        arr1[idx] = arr2[idx];
      }
    }
  }
  return arr1;
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Following is my version of independent array_merge (compact):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="coMULTI">/***
* Simulate php array_merge function
*
... [more] * @param {Object/Array} arr1
* @param {Object/Array} arr2
* var a1 = {'aa':100, 'bb':2, 'cc':[6,7], 'dd':[12,13], 'ee':{'15':15,'16':16}};
* var b1 = {'xx':101, 'bb':5, 'cc':8, 'dd':[14,15], 'ee':{'17':17,'18':18}};
* var c = array_merge(a1, b1);
* console.log(c) [in firebug]
* Output: {'aa':100, 'bb': 5, 'cc':[6,7], 'dd':[12,13,14,15], 'ee':{'15':15,'16':16,'17':17,'18':18}, 'xx':101}
*/</span>
<span class="kw2">var</span> array_merge <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span>arr1<span class="sy0">,</span> arr2<span class="br0">&#41;</span><span class="br0">&#123;</span>
  <span class="kw1">if</span><span class="br0">&#40;</span><span class="br0">&#40;</span>arr1 <span class="sy0">&amp;</span>amp<span class="sy0">;&amp;</span>amp<span class="sy0">;</span> <span class="br0">&#40;</span>arr1 <span class="kw1">instanceof</span> Array<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">&amp;</span>amp<span class="sy0">;&amp;</span>amp<span class="sy0">;</span> <span class="br0">&#40;</span>arr2 <span class="sy0">&amp;</span>amp<span class="sy0">;&amp;</span>amp<span class="sy0">;</span> <span class="br0">&#40;</span>arr2 <span class="kw1">instanceof</span> Array<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
    <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> idx <span class="kw1">in</span> arr2<span class="br0">&#41;</span> <span class="br0">&#123;</span>
      arr1.<span class="me1">push</span><span class="br0">&#40;</span>arr2<span class="br0">&#91;</span>idx<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
  <span class="br0">&#125;</span><span class="kw1">else</span> <span class="kw1">if</span><span class="br0">&#40;</span><span class="br0">&#40;</span>arr1 <span class="sy0">&amp;</span>amp<span class="sy0">;&amp;</span>amp<span class="sy0">;</span> <span class="br0">&#40;</span>arr1 <span class="kw1">instanceof</span> Object<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">&amp;</span>amp<span class="sy0">;&amp;</span>amp<span class="sy0">;</span> <span class="br0">&#40;</span>arr2 <span class="sy0">&amp;</span>amp<span class="sy0">;&amp;</span>amp<span class="sy0">;</span> <span class="br0">&#40;</span>arr2 <span class="kw1">instanceof</span> Object<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
    <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw2">var</span> idx <span class="kw1">in</span> arr2<span class="br0">&#41;</span><span class="br0">&#123;</span>
      <span class="kw1">if</span><span class="br0">&#40;</span>idx <span class="kw1">in</span> arr1<span class="br0">&#41;</span><span class="br0">&#123;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> arr1<span class="br0">&#91;</span>idx<span class="br0">&#93;</span> <span class="sy0">==</span> <span class="st0">'object'</span> <span class="sy0">&amp;</span>amp<span class="sy0">;&amp;</span>amp<span class="sy0">;</span> <span class="kw1">typeof</span> arr2 <span class="sy0">==</span> <span class="st0">'object'</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
          arr1<span class="br0">&#91;</span>idx<span class="br0">&#93;</span> <span class="sy0">=</span> array_merge<span class="br0">&#40;</span>arr1<span class="br0">&#91;</span>idx<span class="br0">&#93;</span><span class="sy0">,</span> arr2<span class="br0">&#91;</span>idx<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span><span class="kw1">else</span><span class="br0">&#123;</span>
          arr1<span class="br0">&#91;</span>idx<span class="br0">&#93;</span> <span class="sy0">=</span> arr2<span class="br0">&#91;</span>idx<span class="br0">&#93;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
      <span class="br0">&#125;</span><span class="kw1">else</span><span class="br0">&#123;</span>
        arr1<span class="br0">&#91;</span>idx<span class="br0">&#93;</span> <span class="sy0">=</span> arr2<span class="br0">&#91;</span>idx<span class="br0">&#93;</span><span class="sy0">;</span>
      <span class="br0">&#125;</span>
    <span class="br0">&#125;</span>
  <span class="br0">&#125;</span>
  <span class="kw1">return</span> arr1<span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Mrsoul</title>
		<link>http://stereointeractive.com/blog/2007/07/19/javascript-array-merge-array_merge/comment-page-1/#comment-289</link>
		<dc:creator>Mrsoul</dc:creator>
		<pubDate>Mon, 11 Aug 2008 09:09:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stereodevelopment.com/2007/07/19/javascript-array-merge-array_merge/#comment-289</guid>
		<description>Another example for merge:
&lt;pre lang=&quot;javascript&quot;&gt;
function array_merge(arr) {
	var merged = arr;
	for (var i = 1; i &lt; arguments.length; i++) {
		merged = merged.concat(arguments[i]);
	}
	return merged;
}

arr4 = array_merge(arr1, arr2, arr3);
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Another example for merge:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">function</span> array_merge<span class="br0">&#40;</span>arr<span class="br0">&#41;</span> <span class="br0">&#123;</span>
	<span class="kw2">var</span> merged <span class="sy0">=</span> arr<span class="sy0">;</span>
	<span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> i <span class="sy0">=</span> <span class="nu0">1</span><span class="sy0">;</span> i <span class="sy0">&amp;</span>lt<span class="sy0">;</span> arguments.<span class="me1">length</span><span class="sy0">;</span> i<span class="sy0">++</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
		merged <span class="sy0">=</span> merged.<span class="me1">concat</span><span class="br0">&#40;</span>arguments<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
	<span class="kw1">return</span> merged<span class="sy0">;</span>
<span class="br0">&#125;</span>
&nbsp;
arr4 <span class="sy0">=</span> array_merge<span class="br0">&#40;</span>arr1<span class="sy0">,</span> arr2<span class="sy0">,</span> arr3<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Meves</title>
		<link>http://stereointeractive.com/blog/2007/07/19/javascript-array-merge-array_merge/comment-page-1/#comment-257</link>
		<dc:creator>Scott Meves</dc:creator>
		<pubDate>Fri, 06 Jun 2008 04:05:22 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stereodevelopment.com/2007/07/19/javascript-array-merge-array_merge/#comment-257</guid>
		<description>concat is KING</description>
		<content:encoded><![CDATA[<p>concat is KING</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rumble</title>
		<link>http://stereointeractive.com/blog/2007/07/19/javascript-array-merge-array_merge/comment-page-1/#comment-109</link>
		<dc:creator>Rumble</dc:creator>
		<pubDate>Tue, 07 Aug 2007 15:05:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stereodevelopment.com/2007/07/19/javascript-array-merge-array_merge/#comment-109</guid>
		<description>You can do this even quicker!

one = one.concat(two);

Job done!</description>
		<content:encoded><![CDATA[<p>You can do this even quicker!</p>
<p>one = one.concat(two);</p>
<p>Job done!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
