Last active
December 28, 2015 19:08
-
-
Save abdulapopoola/7547693 to your computer and use it in GitHub Desktop.
jQuery Map Example
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
var squareNumbers = function (number) { | |
return number * number; | |
}; | |
var numbers = [1,2,3,4]; | |
var squares = $.map(numbers, squareNumbers); | |
//logs [1,4,9,16] | |
console.log(squares); | |
///if you need the index of the element in the array | |
getAllEvenIndices = function(number, indexInArray){ | |
if(indexInArray % 2 === 0) return number | |
} | |
numbers = [1,2,3,4]; | |
evenIndexedNumbers = $.map(numbers, getAllEvenIndices); | |
//logs [1,3] | |
console.log(evenIndexedNumbers); | |
///Using the native JS array map property might be easier | |
squares = numbers.map(squareNumbers); | |
//logs [1,4,9,16] | |
console.log(squares); | |
//Unfortunately there is no support for the index in the native JS map | |
numbers.map(evenIndexedNumbers); //!!Throws a type error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment