Skip to content

Instantly share code, notes, and snippets.

@jjsub
Created December 22, 2016 19:53
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 jjsub/92a5080587d2c64e5625c3f90cde00b0 to your computer and use it in GitHub Desktop.
Save jjsub/92a5080587d2c64e5625c3f90cde00b0 to your computer and use it in GitHub Desktop.
//Explaning call in map
Using map generically
This example shows how to use map on a String to get an array of bytes in the ASCII encoding representing the character values:
var map = Array.prototype.map;
var a = map.call('Hello World', function(x) {
return x.charCodeAt(0);
});
// a now equals [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
Using map generically querySelectorAll
This example shows how to iterate through a collection of objects collected by querySelectorAll. In this case we get all selected options on the screen and printed on the console:
var elems = document.querySelectorAll('select option:checked');
var values = Array.prototype.map.call(elems, function(obj) {
return obj.value;
});
Using map to reverse a string
var str = '12345';
Array.prototype.map.call(str, function(x) {
return x;
}).reverse().join('');
// Output: '54321'
// Bonus: use '===' to test if original string was a palindrome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment