jQuery concat() method for combining two javascript arrays

by Developer 31. January 2010 15:59

In my previous article, I explained about how to split javascript array. In this article I am explaining about how to combine two javascript arrays by using one of the jQuery methods.

 

jQuery provides us one simple method called concat() which combines two javascript arrays and produce one new array which contains all items of remaining two javascript arrays.

 

Syntax of jQuery concat() method:

                                                        arr3 = arr1.concat(arr2); //arr1,arr2 and arr3 are javascript arrays

 

Here arr1 is invoking the concat() method and we are passing arr2 as a parameter to that method. That results one new array arr3 by combining two arrays arr1, arr2.

 

I am explaining jQuery concat() method with simple example.

 

<html>
    <head>
        <title>javascript arrays using jQuery concat() method</title>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                var elems1 = ['Asp.Net', 'Vb.Net', 'C#.Net'];
                var elems2 = ['Asp.Net MVC', 'Sharepoint'];
               
                //First array
                $('#div1').html(elems1.join('<br/>'));

                //Second array
                $('#div2').html(elems2.join('<br/>'));

                //Adter combining two arrays
                var elems = elems1.concat(elems2);
                $('#div12').html(elems.join('<br/>'));

            });   
        </script>
    </head>
    <body>
        <p><b>First Array</b></p>
        <div id="div1"></div>
        <p><b>Second array</b></p>
        <div id="div2"></div>
        <p><b>After combining the two arrays</b></p>
        <div id="div12"></div>
    </body>
 </html>

 

As shown above, we have two arrays elems, elems2. We are assigning these two arrays to two different <div> tags div1 and div2.

 var elems = elems1.concat(elems2); is combining the two arrays elems1, elems2 and assigning the result array to elems. Here elems1 is invoking the concat() jQuery method and we are passing the elems2 array as a parameter. The resulting array contains all elements of elems1, elems2 arrays.

 

Download source code: jQuery_concat.zip (35.78 kb)

Comments

6/25/2010 9:24:21 AM #

Elbert Pavon

Puppets in my idea were the best way to teach children, as least when I was growing up!

Elbert Pavon

6/27/2010 8:30:10 PM #

Forerunner 405 Review

How are you, I bumped into this web site by mistake when I was exploring on Google then I went on your site. I have to say your site is cool I love your theme! Right now I don’t have the free-time at the current moment to look through your website web sitebut I have bookmarked it. I will be back in a day or two. Thanks for a great site.

Forerunner 405 Review

7/4/2010 12:34:13 AM #

mobile monopoly review

I always enjoy your posts, your real, and some times funny as well. keep it up!

mobile monopoly review

7/4/2010 3:10:00 AM #

göğüs estetiği

Why this web site do not have other languages support?

göğüs estetiği

7/7/2010 5:42:34 AM #

accutane lawsuit

It's a shame what Accutane has done to so many people. Hopefully those affected clear those hurdles.

accutane lawsuit

7/8/2010 1:41:38 PM #

Top 10 interview questions

Who admins of this site? I'd like to exchange links.  Looking at the amount of hits that jQuery concat() method for combining two javascript arrays gets and it'd be worth it for me to trade links.  It's good to get ahead of the competition and all.  And, most of my links come from web sites just like this place.

Top 10 interview questions

7/9/2010 6:22:33 AM #

male enhancement

What regarding middle aged guys that still have sex daily? I think there are less of us out there. Sounds like several guys need somewhat bit of help, and also a kick within the butt to get operating upon it.

male enhancement

7/12/2010 10:24:04 AM #

registry cleaners

I believe most people would agree with your article. I am going to bookmark this web site so I can come back and read more posts. Keep up the good work!

registry cleaners

Add comment




  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading



Page List

    Tag cloud