Skip to content

Instantly share code, notes, and snippets.

@gusaaaaa
Last active May 9, 2016 14:39
Show Gist options
  • Save gusaaaaa/5664488 to your computer and use it in GitHub Desktop.
Save gusaaaaa/5664488 to your computer and use it in GitHub Desktop.
ECMAScript 5.1 tricks
// one-liner to build a sequence of numbers
// source: http://ariya.ofilabs.com/2013/01/es6-and-array-comprehension.html
Array.apply(0, Array(3)).map(function(x, y) { return y }); // [0, 1, 2]
// one-liner to filter elements
// source: http://ariya.ofilabs.com/2013/01/es6-and-array-comprehension.html
[1,4,2,3,-8].filter(function(i) { return i < 3 }); // [1, 2, -8]
// one-liner to filter out even numbers
// source: http://ariya.ofilabs.com/2013/01/es6-and-array-comprehension.html
[0,1,2,3,4,5,6].filter(function(x,y) { return y & 1 });
// one-liner to filter out vowels
// source: http://ariya.ofilabs.com/2013/01/es6-and-array-comprehension.html
['X','A','B','E','Z'].filter(function(s) { return 'AEUIO'.indexOf(s) < 0 });
// array-like structure to array
a = { 0: 'A', 1: 'B', length: 2 }
[].slice.call(a) // ['A', 'B']
@DrummerHead
Copy link

I think Mustafa is a pretty cool guy. Eh does SEO and doesn’t afraid of anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment