Skip to content

Instantly share code, notes, and snippets.

@abdulapopoola
Last active December 28, 2015 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdulapopoola/7547693 to your computer and use it in GitHub Desktop.
Save abdulapopoola/7547693 to your computer and use it in GitHub Desktop.
jQuery Map Example
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