Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
101
rated 0 times [  108] [ 7]  / answers: 1 / hits: 38570  / 16 Years ago, thu, april 2, 2009, 12:00:00

I am trying to get a better understanding of the jQuery.map function.



So in general terms .map takes a array and maps it to another array of items.



easy example:



$.map([0,1,2], function(n){
return n+4;
});


results in [4,5,6]



I think I understand what it does. I want to under why would someone need it. What is the practical use of this function? How are you using this in your code?


More From » jquery

 Answers
14

$.map is all about converting items in a set.



As far as the DOM, I often use it to quickly pluck out values from my elements:



var usernames = $('#user-list li label').map(function() {
return this.innerHTML;
})


The above converts the <label> elements inside a list of users to just the text contained therein. Then I can do:



alert('The following users are still logged in: ' + usernames.join(', '));

[#99763] Thursday, March 26, 2009, 16 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
byron

Total Points: 616
Total Questions: 101
Total Answers: 91

Location: Reunion
Member since Wed, Apr 14, 2021
3 Years ago
byron questions
Wed, Jan 26, 22, 00:00, 2 Years ago
Sat, Jun 6, 20, 00:00, 4 Years ago
Thu, Nov 7, 19, 00:00, 5 Years ago
Wed, Sep 11, 19, 00:00, 5 Years ago
;