jQuery splice() method for splitting the javascript array

by Developer 31. January 2010 07:55

We have splice() method in jQuery to split the javascript array in a simple way. This will helpful when you want to perform different actions on the different parts of javascript array.

 

jQuery splice() method requires two parameters, the first parameter is the index of the item from where to start splitting and second parameter is the number of items to be removed from the array after the specified index.

 

jQuery splice() method syntax:

                                                    subarray = array.splice(i,n);

 

Where i the index of the item from where we have to start the splitting and n is the number of elements to be removed from original array after the specified index i.

 

Here I am explaining simple example by using splice() method.

 

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

                //spliting the array
                var elems2 = elems.splice(1, 2);

                //split part
                $('#div1').html(elems2.join('<br/>'));

                //remaining part
                $('#div2').html(elems.join('<br/>'));
           
            });   
        </script>
    </head>
    <body>
        <p>Original Array</p>
        <div id="divo"></div>
        <p>1st Part of the array</p>
        <div id="div1"></div>
        <p>2nd Part of the array</p>
        <div id="div2"></div>
    </body>
 </html>

 

In the above code we have original array "elems" and we are assigning this array to the <div> tag whose id is "divo" one by one by using  $('#divo').html(elems.join('<br/>'));


We are splitting this array by using splice method as shown below.

                              var elems2 = elems.splice(1, 2);


Here we are splitting the "elems" array and assigning part of the array to the new array "elems2". When we split the some part of from the main array and assigning to the new array, the remaining part will be assign to the main array automatically. That means here remaining part will be placed in the main array "elems" only.

 

After splitting the array we are assigning the first part to the "div1" <div> tag by using $('#div1').html(elems2.join('<br/>')); and remaining part to the "div2" <div> tag by using $('#div2').html(elems.join('<br/>'));.

 

Download source code: jQuery_splice.zip (35.79 kb)

Comments

5/12/2010 7:48:21 AM #

Cochise Nandkeolyar

Browsing a number of blogengine.net blogs - it appears a excellent platform. Best wishes to your website, cheers!

Cochise Nandkeolyar

5/12/2010 7:25:45 PM #

Wilhemina Sluss

Ok, how would i add your rss feed to my reader, sorry this is all new stuff for me so please be patient with me, thanks.

Wilhemina Sluss

5/16/2010 12:18:14 AM #

corner desks home

This heating tips are very useful especially the advice to not to use stove to heat the room. Its very harmful.

corner desks home

5/17/2010 2:09:16 PM #

web design

I will employ of some your principles...

web design

5/18/2010 5:05:08 AM #

HELEN BOND

hello great website yea nice work our review website will soon be adding reviews on websites and add them to our blogs as the top best 100 blogs to visit we also do reviews on Consumer Reports reviews  all types of reviews thanks

HELEN BOND

5/21/2010 7:39:48 PM #

telephone lyrics by lady gaga

I found this post while surfing the net lyric updates. Thanks for sharing I'll be back regularly.

telephone lyrics by lady gaga

5/23/2010 1:27:38 AM #

debt

I have read a lot of good stuff on this blog. Its Definitely worth bookmarking for revisiting.

debt

5/23/2010 2:57:39 PM #

tiffany lamps for sale

It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks

tiffany lamps for sale

5/24/2010 1:24:20 AM #

country table lamp

Like my old pa used to say, education is the key to unlock the golden door of freedom. A taste for irony has kept more hearts from breaking than a sense of humor, for it takes irony to appreciate the joke which is on oneself. Tyra

country table lamp

5/29/2010 10:40:00 AM #

jillian michaels diet

Thanks for piecing this together - this is a great article for those of us with our heads buried in the keyboard all day.

jillian michaels diet

5/29/2010 8:54:16 PM #

Florrie Hair

I don’t usually reply to posts but I will in this case.

Florrie Hair

5/29/2010 11:32:49 PM #

Bev Benigni

Hi there, I found your blog via Google while searching for muscle building and your post looks very interesting for me.

Bev Benigni

5/31/2010 1:58:48 PM #

deal a day tracker

Nice post, grabbed your feed &amp;  I have  to post a update on our internal blog and I will put a blerb to this post . It is related to a idea we are toying with  on , I will forward  you a link.

deal a day tracker

6/2/2010 3:56:34 PM #

sklepy internetowe

Thanks for sharing, please keep an update about this info. love to read it more. i like this site too much. Good theme ;).

sklepy internetowe

6/3/2010 12:00:45 AM #

Sarah | Custom PC

This is very interesting. Keep blogging the way you do :)

Sarah | Custom PC

6/3/2010 10:59:30 AM #

Budget Computers

This is very interesting. I actually enjoy your writing style and your word choice more than anything :)

Budget Computers

6/4/2010 7:16:01 AM #

Sara | Custom Computers Canada

This is very interesting. I actually enjoy your writing style and your word choice more than anything :)

Sara | Custom Computers Canada

6/7/2010 11:56:45 PM #

Kerri Herriage

This is the cooooooooooolest simpson

Kerri Herriage

6/10/2010 5:40:50 AM #

Meds

Thank you! I constantly needed to write on my website something like that. Can I take a fragment of your post to my site?

Meds

6/10/2010 3:30:17 PM #

Amado Ko

Throughout the particular joining (my spouse and i flat-out will not likely execute), just one mandatory of which prevents people caused by reviews your content material by yourself. Generally certainly not look Concerning in order to create, My own spouse may well. The “great document!” viewpoint without any ingredient might be tedious inside regarding period.

Amado Ko

6/11/2010 7:34:02 AM #

free movies online to watch

Hello. This is a very nice weblog. You have done a great job! Congrats :)

free movies online to watch

6/14/2010 8:06:12 AM #

kat von d beauty products

I don’t usually reply to posts but I will in this case. WoW big thumbs up for this one!

kat von d beauty products

6/14/2010 9:30:01 AM #

Bobbye Levanger

cool i love those, good work!

Bobbye Levanger

6/15/2010 2:53:54 AM #

play games

good piece, I was wondering if I could use this post on my website. please let me know

play games

6/15/2010 6:57:14 PM #

Maybelle Sorn

Good shout, sometimes it takes someone to say the blindingly obvious for everyone else to see it

Maybelle Sorn

6/16/2010 8:04:22 AM #

get ex back

Commenting is survival of the fittest. The chance of your comments are being approved depends on the current situation of the comments field itself. If you have seen many good comments, then you have to make a good one too, to make it approved. If you see no comments at all, giving lame comments might already makes the writer happy :D What do you think?

get ex back

6/16/2010 8:20:43 PM #

abercrombie

I always follow this weblog, I find it very well structured. Congratulations to the writer of the site

abercrombie

6/16/2010 8:21:03 PM #

abercrombie and fitch

Very frequently I go to this blog. It very much is satisfying to me. Thanks the author

abercrombie and fitch

6/17/2010 12:55:50 AM #

Leona Chad

Hi everyone. Just a quick note. If you've seen the recent floods and want to help, please go to RedCross.org. They need donations and volunteers.

Leona Chad

6/18/2010 10:03:44 AM #

Brigida Rappleye

Thanks for this great post.This is first time on this website and really found it so useful.

Brigida Rappleye

6/18/2010 6:00:44 PM #

starcraft 2 beta

Any of you guys play PC Games?  If so I don't suppose any of you have been looking into Blizzard's new RTS?

starcraft 2 beta

6/20/2010 7:29:53 PM #

abercrombie and fitch

I always be a fan of this website, I find it really well structured. Congratulations to the creator of the website

abercrombie and fitch

6/20/2010 9:12:32 PM #

abercrombie

Very habitually I go to this internet site. It very greatly is satisfying to me. Thanks the author

abercrombie

6/20/2010 11:45:43 PM #

babies  twins strollers

Yes! thought your post is a time saver! please visit aour blog. thank you for visit. i was clicked your adds for you win some cash :)

babies twins strollers

6/21/2010 3:02:02 AM #

Fat Burning Furnace

It’s a fair point to use spam-protection and comment-anti-spam systems like Akismet. If your blog is a personal thing then it’s only right that our comments have personality to them. After all, personallity is what makes reading blogs interesting. Keep posting and deleting bad comment that are just negative or doesn't add to the conversation!

Fat Burning Furnace

6/21/2010 4:44:47 AM #

verifone manual

I know you are using Microsofts BlogEngine.NET but can you please tell me which database system that you are using?  I've been looking all over and wanted one like the one here at %BLOGSITE% I came from wordpress and am trying to figure out BlogEngine.

verifone manual

6/23/2010 7:08:23 PM #

Edna Poitier

Many people don’t understand the effects misleading effects. I knew what you meant. It can be more effective to give people little opinion with facts here. I recognize this problem but with little clue, so we must face the topic together. With new assurance, we hope that the skill of other categories can be found together. Nice people can work with great people, and great resource is important for us in analyzing the case. I’ve studied for more than 4 years to analyze them, but the fact still can’t be discovered. In order to do that, we must deal with the changes of management resources. With so many resources here, we can develop new ideas for solving to problem. Let’s give opinion, let’s discuss them, we can take charge with our resources. The last one but not least, I support your analyzes to face new one. With many resources that we can collect, all of them can be handled well.

Edna Poitier

6/24/2010 8:11:09 PM #

p90x workout

I am sure you have spent a lot of time to give this quality article to us. I do not know how to thank you.

p90x workout

6/24/2010 9:13:37 PM #

Hypercom T7Plus Manual

This is the first I've seen this info, however it's better put this way.  Ya know?  Been going crazy trying to find how or where is the best place to learn about how to prepare for this.

Hypercom T7Plus Manual

6/24/2010 10:21:07 PM #

sennheiser australia

Striking thoughts here. Are you positive this is the accurate way to look at it though? My own personal experience is that we should pretty much live and let live because what one person surmises as just -- another person simply does not. Individuals are going to do what they want to do. In the end, they always do. The best we can yearn for is to reveal a few things here and there that hopefully, allows them to make just a little better informed decision. Otherwise, great post. You're definitely making me think! --Mark

sennheiser australia

6/25/2010 10:38:46 AM #

Lynsey Bidell

Wow fantastic work and massive respect - Good to have your posts. Nice!

Lynsey Bidell

6/29/2010 12:27:55 AM #

abercrombie

I be able to not agree on everything, but the theme is really interesting. Congratulations!

abercrombie

6/29/2010 9:57:03 AM #

text messagingsoftware

Thought you may like to know I came to this webpage from the first page of aGoogle seach. Great Job. I know how tidious it is to get your your website on the first page of a search. Ive downloaded ebook after ebook and now got <A href="http://asbestos-mesothelioma-attorney.us/">asbestos lung disease</A> on the first page. sheesh only took 2months!!

text messagingsoftware

6/29/2010 11:37:32 AM #

text messaging software

Thought you may like to know I found your site from the first page of aTwitter seach. Nice work. I know how difficult it is to get your your website on the first page of a search. Ive read program after program and now got <A href="http://textmessagingsoftware.org/category/text-message-online/">text messaging software</A> on the first page. omg only took 6months!!

text messaging software

6/29/2010 2:45:13 PM #

starcraft 2 beta

Who here is into RTS games?

starcraft 2 beta

6/30/2010 3:15:02 AM #

Sarah Mcintyre

Great article, a great deal of handy knowledge.

Sarah Mcintyre

6/30/2010 9:37:07 AM #

Proven Penis Enlargement

What theme is this webpage site using? I know it is a blogging site engine web page but I�ve never seen this palette before.

Proven Penis Enlargement

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

mobile monopoly review

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

mobile monopoly review

7/6/2010 12:48:31 PM #

the sonoma diet

I think 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!

the sonoma diet

7/7/2010 1:22:39 AM #

Hyacinth Chadwell

I very much liked reading your site.  Great content.  Please keep posting such awesome cotent.

Hyacinth Chadwell

7/8/2010 1:44:40 PM #

Top 10 Job Interview Questions

Hi. I just noticed that your site looks like it has a few code problems at the very top of your website's page. Is it an older version of BE?  I'm not sure if everybody is getting this same bugginess when browsing your site? I am employing a totally different browser than most people, referred to as Chrome, so that is what might be causing my problem? I just wanted to make sure you knew. Thanks for posting some great postings and I will try to return back with a completely different browser to check things out!

Top 10 Job Interview Questions

7/9/2010 3:24:22 AM #

ncaa

The Big 10 Football Nation Forum can be a website where it is possible to examine just about anything in regards to the NCAA Big 10 Football Conference. You may also examine about other issues like other NCAA Football Conferences, other sports, everyday chit chat, and many other topics. There is also a special VIP Section exactly where you might acquire, market, trade, or have sports bets with other members. http://www.big10footballnation.net/forums/forum.php

ncaa

7/9/2010 6:24:45 AM #

male enhancement

Longer sex + larger penis = happy wife. It works with regard to me, anyway.

male enhancement

7/12/2010 8:54:03 AM #

engagement rings

White gold is usually my favorite, it holds up actually well.

engagement rings

7/12/2010 4:26:26 PM #

jasminelive

I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well.

jasminelive

7/15/2010 1:29:30 PM #

Denny Cadoff

<a href="http://www.bloggingtothebankbonus.info">blogging to the bank</a>can help you proft this site.  Not certain issues previously looked out.

Denny Cadoff

7/16/2010 3:18:55 AM #

Allofmp3

This is my first time I have visited here. I found a lot of interesting stuff in your blog. From the tons of comments on your articles, I guess I am not the only one! keep up the good work.

Allofmp3

7/27/2010 11:02:30 AM #

beach resorts

My brother and I were just discussing this very topic, he's frequently attempting to prove me wrong. Your current view on this is wonderful and just how I truly feel. I just now emailed my brother this site to show him your own perspective. After looking over your weblog I bookmarked and will be coming back to read your messages!

beach resorts

7/27/2010 7:00:38 PM #

Xavier Vanwyngaarden

pls more photoes

Xavier Vanwyngaarden

7/28/2010 7:31:11 AM #

Internorm

Come on guys, let's keep it clean. This is a good blog. Good info mate.

Internorm

7/28/2010 7:44:33 PM #

Alica Wilda

Hey - great web site, simply trying round some blogs, appears a really nice platform you might be using. I am at present utilizing Wordpress for a few of my websites however trying to change one among them over to a platform much like yours as a trial run. Something particularly you'd advocate about it?

Alica Wilda

7/29/2010 11:40:11 AM #

Add comment




  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading



Page List

    Tag cloud