Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.map - Practical uses for the function?

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?

like image 897
RedWolves Avatar asked Apr 02 '09 02:04

RedWolves


People also ask

What is the use of map function in jQuery?

The $.map() method applies a function to each item in an array or object and maps the results into a new array. Prior to jQuery 1.6, $.map() supports traversing arrays only. As of jQuery 1.6 it also traverses objects.

What is the primary purpose of the array map () function?

The primary purpose of the array map() function is that itThe map() method passes each element of the array on which it is invoked to the functionyou specify, and returns an array containing the values returned by that function.

What is the function of map in JS?

map() creates a new array from calling a function for every array element. map() calls a function once for each element in an array. map() does not execute the function for empty elements.

What is map function with example?

map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.) Syntax : map(fun, iter) Parameters : fun : It is a function to which map passes each element of given iterable. iter : It is a iterable which is to be mapped.


2 Answers

Mapping has two main purposes: grabbing properties from an array of items, and converting each item into something else.

Suppose you have an array of objects representing users:

var users = [   { id: 1, name: "RedWolves" },   { id: 2, name: "Ron DeVera" },   { id: 3, name: "Jon Skeet" } ]; 

Mapping is a convenient way to grab a certain property from each item. For instance, you can convert it into an array of user IDs:

var userIds = $.map(users, function(u) { return u.id; }); 

As another example, say you have a collection of elements:

var ths = $('table tr th'); 

If you want to store the contents of those table headers for later use, you can get an array of their HTML contents:

var contents = $.map(ths, function(th) { return th.html(); }); 
like image 82
Ron DeVera Avatar answered Sep 30 '22 02:09

Ron DeVera


$.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(', ')); 
like image 35
JPot Avatar answered Sep 30 '22 03:09

JPot