jQuery map() function to perform action on each item of javascript arrays

by Developer 24. January 2010 14:11

To perform action on each item of array in javascript requires for loop. That means you have to iterate every item of array by using for loop.

 

Instead of that jQuery provide us simple function called map(), where you can process each item of array without using for loop.

 

map() function syntax:

                                     $.map("your array","callbackfunction")

 

The "callbackfunction" requires two arguments. First argument is the item of the array and second argument is the index in the array. The map() function returns the another array.

 

In the below code, you can find how to use map() function.


<html>
    <head>
        <title>javascript arrays using jQuery map() function</title>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                var elems = ['Asp.Net', 'Vb.Net', 'C#.Net', 'Asp.Net MVC', 'Sharepoint']
                elems = $.map(elems, function(n, i) {return (i + 1 + "." + n); });
                $('#divmap').html(elems.join("<br/>"));
            });   
        </script>
    </head>
    <body>
      <p>Array Elements using map() jQuery function</p>
      <div id="divmap"></div>
    </body>
</html>


In the above code we have a array "elems" which has some items.

$.map(elems, function(n, i) {return (i + 1 + "." + n); }) will produce the another array by assigning numbers to the each element in the array "elems".

After indexing each item in the array, we are joining the items by using "<br/>" as separator and assigning to the <div> tag whose id is "divmap" as html.

 

Download source code: jQuery_map.zip (35.69 kb)

Comments

6/16/2010 4:29:01 PM #

Israel Zabek

Fantastic article, a lot of useful material.

Israel Zabek

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

Nery Sarvis

I was rather glad to find this website. I wanted to appreciate this outstanding read!! I most definitely relished every little bit  and I have you bookmarked to check out fresh new stuff you publish.

Nery Sarvis

6/24/2010 3:54:16 PM #

stop panic attacks

The official quality for the purpose of panic attacks is often as accepts: Panic attacks happen to be rapid along with recurrent spikes in serious plus vast concern.

stop panic attacks

6/25/2010 8:26:19 AM #

diets that work

It’s hard to find sharp writers on this topic, but you seem like you know what you’re talking about! Thanks.

diets that work

7/2/2010 5:46:55 AM #

wwe jeff hardy toy

Your attitude is an expression of your values,beliefs and expectations. Brian Tracy

wwe jeff hardy toy

7/7/2010 7:17:32 PM #

Peter Holdeman

You're fascinated about understanding all you may about this. We're completely happy you discovered us. On this site, we have introduced together an enormous array of data and assets on this.

Peter Holdeman

7/7/2010 7:18:30 PM #

Janeth Wegrzyn

I adore this web page

Janeth Wegrzyn

7/8/2010 1:42:53 PM #

Top 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 Blog Engine?  I'm not sure if everybody is getting this same bugginess when browsing your blog? I am employing a totally different browser than most people, referred to as Chrome, so that is what might be causing it? I just wanted to make sure you knew. Thank you for posting some great postings and I will try to return back with a completely different browser to check things out!

Top Interview questions

7/9/2010 6:23:04 AM #

male enhancement

I feel bad with regard to guys whom don't have significantly of a sex life. They're truly missing out on a beautiful part of life, along with I'm creating sure I'm not missing it myself.

male enhancement

7/9/2010 2:41:08 PM #

Oswaldo Yearego

Hello, I was researching the internet  & I revealed your web site. Keep up the wonderful work.

Oswaldo Yearego

7/12/2010 2:37:06 PM #

Louie Domanski

I believe you should pick up a little the design and usability of the blog.

Louie Domanski

7/12/2010 10:43:23 PM #

Duane Ritson

Great blog, keeping me from working

Duane Ritson

7/15/2010 2:25:36 AM #

Legalsounds

I just couldnt leave your website before saying that we really enjoyed the useful information you offer to your visitors... Will be back often to check up on new stuff you post!

Legalsounds

7/22/2010 8:58:09 AM #

the diet solution

Thanks very much for the information. I have been searching for this for awhile with Yahoo and it has been a real chore.

the diet solution

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

Earlie Oskins

I encounterd difficulties reading your blog with safari browser, you'll higher update your weblog

Earlie Oskins

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

Joan Poer

I posted your piece of writing to my myspace profile.

Joan Poer

7/29/2010 10:56:12 AM #

medifast diet review

It’s hard to find sharp bloggers on this topic, but you seem like you know what you are talking about! Thanks.

medifast diet review

7/30/2010 9:02:42 PM #

Benjamin Boshnack

Whats happening, I found this site about javascript array by mistake when I was browsing Yahoo after that I arrived to your web site. I need to say your internet site is interesting I really like your theme! Currently I don�t have the free time at the present moment to fully browse your sitebut I have bookmarked it. I will be back in a day or two. Thanks for a good site.

Benjamin Boshnack

8/1/2010 5:28:46 AM #

Bari Lukasiewicz

You are occupied with figuring out all you'll be able to about this. We're pleased you found us. On this web site, we've brought together an enormous array of information and sources on this.

Bari Lukasiewicz

8/2/2010 10:27:31 PM #

Sherley Prosper

Hey - nice website, just looking around some blogs, appears a really nice platform you might be using. I am at the moment using Wordpress for a few of my websites but looking to change one in every of them over to a platform similar to yours as a trial run. Something specifically you would recommend about it?

Sherley Prosper

8/2/2010 10:27:31 PM #

Young Guest

that is just what I was in search of

Young Guest

8/3/2010 6:18:27 PM #

Don Isaza

Can I quote you on my web site if I hyperlink back to your website?

Don Isaza

8/3/2010 6:18:52 PM #

Chester Badder

Helpful site, where did u come up with the information with this posting? I'm happy I discovered it though, ill be checking back soon to find out what supplementary content you include.

Chester Badder

8/4/2010 7:58:54 AM #

Darius Lolagne

I did not see a button wherever however do you could have promoting? I've a number of blogs in the identical area of interest and I want to add my button somwhere on your webiste.

Darius Lolagne

8/5/2010 11:16:18 AM #

used stationary bikes

great story

used stationary bikes

8/6/2010 1:54:49 AM #

Mercedez Forcade

You should really moderate the comments here

Mercedez Forcade

8/6/2010 8:08:30 AM #

Armando Enns

While this subject may be very sensitive for most people, my opinion is that there needs to be a middle or common floor that we all can find. I do admire that youve added relevant and intelligent commentary here though. Thank you!

Armando Enns

8/8/2010 2:16:32 PM #

Carlos Poisel

That is exciting, post more often!

Carlos Poisel

8/10/2010 8:07:28 AM #

Arthur Kirkendall

Super-Duper site! I am loving it!! Will come back again - taking you feeds also, Thanks.

Arthur Kirkendall

8/13/2010 4:21:09 PM #

promo items

Nice post!  I always thought javascript array seemed relevant!  Bookmarking this on Digg.

promo items

8/16/2010 12:10:43 AM #

DR Avraham Glattman

Hi, I�m rather impressed by your blogging skills about javascript array . Please make contact with me if a part-time running a blog work sounds like some thing you be considering

DR Avraham Glattman

8/16/2010 12:11:04 AM #

Vern Katsaounis

Dude.. I'm not much into reading about javascript array , but somehow I got to read plenty of articles on your webpage. Its amazing how interesting it is for me to visit you very often.

Vern Katsaounis

8/16/2010 12:11:39 AM #

Syreeta Brannin

I needed to thanks for this great read!! I undoubtedly enjoyed every little little bit of it. I've you bookmarked your website to check out the brand new stuff you post.Should you're enthusiastic about having a guest blog poster please reply and let me know. I will offer you unique content material in your webstie, thanks.

Syreeta Brannin

8/16/2010 12:12:09 AM #

Bennett Maybury

Maby considere to write a javascript array company to put here adv

Bennett Maybury

8/18/2010 3:12:40 AM #

Doctor Avraham Glattman

thanx javascript array

Doctor Avraham Glattman

8/18/2010 3:12:41 AM #

Lane Edgerly

thanx javascript array

Lane Edgerly

9/4/2010 7:53:35 AM #

dfrgvr

http://www.androidsystem.org/androids.html

dfrgvr

9/4/2010 8:02:30 PM #

unfallversicherung vergleich

Congratulations, this was  a brilliant article. In theory I would like to write like this too. It takes time to creat that excellent and in addition lots of effort to write  a brilliant article.

unfallversicherung vergleich

9/5/2010 8:17:29 PM #

Rajan Singh Rawat

Hi, Thanks for sharing the valuable information. Your posting is unique and very simple to understand and very knowledgable. Nice article

Rajan Singh Rawat

Add comment




  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading



Page List

    Tag cloud