UPDATE 1:
Here is more of the script:
$(".favorites").sortable(
{update:function() {
var that = this;
var urls = "";
var texts = "";
$.map($(".favorites a"), function(elt) {
urls += "url=" + elt.href + "&";
texts += "text=" + $(elt).html() + "&";
});
$.ajax({
url: 'favorites.asmx/save',
type: 'POST',
data: { strItems:$(that).sortable("serialize",{key:'item'}), strURLs:urls.substr(0,urls.length - 1), strTexts:texts.substr(0,texts.length - 1) },
error: function(xhr, status, error) {
console.log(xhr.responseText);
},
success: function() {
console.log("Values sent:- strURLs: " + urls.substr(0,urls.length - 1));
}
});
ORIGINAL QUESTION:
I have the following script which works, but I don’t understand it:
$.map($(".favorites a"), function(elt) {
urls += "url=" + elt.href + "&";
texts += "text=" + $(elt).html() + "&";
});
I understand the basic examples in this link: http://api.jquery.com/jQuery.map/, but I don’t understand the script I have posted above.
The purpose of
mapis to create an array by taking each of the elements of the array or object you pass into it and filtering them through the function you give it; the function’s return values are collected together into the resulting array, which is the return value ofmap.That code isn’t doing that. It’s not returning anything from the iterator, and it’s not using the resultant array. Basically it’s just re-invented
.eachby abusingmap. That script should be:(Note that I’m assuming here that
urlsandtextshave been declared and initialized prior to the code you quoted.)Here’s an example of a correct use of
map: