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

Add comment




  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading



Page List

    Tag cloud