Difference between each and map in jquery
HTML-код
- Опубликовано: 10 сен 2024
- Link for all dot net and sql server video tutorial playlists
www.youtube.co...
Link for slides, code samples and text version of the video
csharp-video-tu...
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our RUclips channel. Hope you can help.
/ @aarvikitchen5572
In this video we will discuss the difference between each and map functions in jquery
$.map
map method can be used as an iterator.
Returns a new array.
The order of callback arguments - element, index.
$.map(elems, function () { element, index }, arg)
Does not have a way to terminate the iteration
$.each
each method is an immutable iterator.
Returns the original array.
The order of callback arguments - index, element
$.each(elems, function () { index, element }, arg)
return false to terminate the iteration
Example : Notice that the callback arguments in the each method are the reverse of the callback arguments in the map function. Also notice that map returns a new array where as each method returns the original array. This proves the point that each method is an immutable iterator where as map is not.
$(document).ready(function () {
var intArray = [1, 2, 3, 4, 5];
function functionA(index, element) {
return element * 5;
}
function functionB(element, index) {
return element * 5;
}
var result1 = $.each(intArray, functionA);
var result2 = $.map(intArray, functionB);
document.write('each = ' + result1);
document.write('<br/>')
document.write('map = ' + result2);
});
Example : Notice that each method terminates the iteration when the element value is 3. The values 3, 4 and 5 are not written to the document. With map method we are not able to break the iteration. When the element value is 3, map method returns false and then continues writing 4 and 5 to the document.
$(document).ready(function () {
var intArray = [1, 2, 3, 4, 5];
$.each(intArray, function (index, element) {
if (element == 3)
return false;
document.write(element + ',');
});
document.write('<br/>');
$.map(intArray, function (element, index) {
if (element == 3)
return false;
document.write(element + ',');
});
});
This is the best awesome lecture I've seen. Thank you so much Venkat!!!
Thank you. You are the best!!!
Is it just me, or is it bad form that $.each() didn't throw some kind of warning when it didn't multiply the value inside the function?
ur videos r amazing.pls upload more about json..
Map method is working fine with me but when I have tried giving argument B in each method, output doesn't get chanced. var result1 = $.each(intArray, functionB); Does it mean that each method callback function can take any order .. this is my full code. var intArray = [4, 6, 2, 10, 8];
function functionA(index, element) {
return element * 5;
}
function functionB(element, index) {
return element * 5;
}
var result1 = $.each(intArray, functionB);
var result2 = $.map(intArray, functionB);
document.write('each = ' + result1);
document.write('')
document.write('map = ' + result2);
I am lil bit confused with this. Correct me if I am wrong.. Just like $.each() and $().each there are $.map() and $(). both the each function takes callback in the form of function(index, element){} but $().map takes function(index, element){} while $.map() takes in the form of function(element, index){}...
maybe you should have included difference between maps in Part 26 difference between each video.
thanks man
Thank you for your efforts wonderful
thank you sir
Thanks sir,
using document.write inside document.ready event is destroying the document contents in FireFox , so is there a solution for it ?
Hi, what should i do if i want to write the result1 and result2 into a div? Thank you~
you can make id in div element . and call that. for eg $("#divid").html('each= '+ result1);
Thanks Sir, Please upload video related to ajax implementation through jquery.
Jyotirmaya Das You are very welcome. Sure, I will record and upload as soon as I can. Thank you very much for your patience.
I have organised all the Dot Net & SQL Server videos in to playlists, which could be useful to you
ruclips.net/user/kudvenkatplaylists?view=1&sort=dd
Slides and Text Version of the videos can be found on my blog
csharp-video-tutorials.blogspot.com
Tips to effectively use my youtube channel.
ruclips.net/video/y780MwhY70s/видео.html
If you want to receive email alerts, when new videos are uploaded, please feel free to subscribe to my youtube channel.
ruclips.net/user/kudvenkat
If you like these videos, please click on the THUMBS UP button below the video.
May I ask you for a favor. I want these tutorials to be helpful for as many people as possible. Please free to share the link with your friends and family who you think would also benefit from them.
can we give any name to function paramiters