Summary
Keywords
Full Transcript
Link for all dot net and sql server video tutorial playlists https://www.youtube.com/user/kudvenkat/playlists?sort=dd&view=1 Link for slides, code samples and text version of the video http://csharp-video-tutorials.blogspot.com/2015/04/difference-between-each-and-map-in.html 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 YouTube channel. Hope you can help. https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1 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 + ','); }); });
