Last active
December 18, 2015 17:58
-
-
Save kovaldn/5821911 to your computer and use it in GitHub Desktop.
Javascript: each & map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // map, each | |
| // ------------------------------------------------------------------------------------- | |
| // map возвращает новый объект, а each - исходный | |
| // отсюда следуюет, что метод each можно включать в цепочку вызова, а метод map - нельзя | |
| // ------------------------------------------------------------------------------------- | |
| // Map позволяет создать новый объект с изменёнными данными (на основе прежних). | |
| // Each позволяет просто пройтись по объекту. | |
| // .each() | |
| .each(callback(index, domElement)) | |
| var heights = []; | |
| $("div").each(function(indx, element){ | |
| heights.push($(element).height()); | |
| }); | |
| $('img').each(function () { | |
| if($(this).width() > 400) { | |
| $(this).fadeOut(3000); | |
| } | |
| }); | |
| // .map() | |
| .map(callback(index, domElement) | |
| var classes = $("div").map(function(indx, element){ | |
| return $(element).attr("class"); | |
| }); | |
| $("p").append( $("input").map(function(){ | |
| return $(this).val(); | |
| }).get().join(", ") ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment