Created
December 29, 2015 13:31
-
-
Save eshacker/39f1fd7f2ec2e17f9ba3 to your computer and use it in GitHub Desktop.
Using arrow functions to generate arrays out of original array.
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
| const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
| console.log("arr: ", arr); | |
| let even = arr.filter(x => x%2 === 0); | |
| console.log("evens: ", even); | |
| let odd = arr.filter(x => x%2 === 1); | |
| console.log("odds: ", odd); | |
| let gt5 = arr.filter(x => x > 5); | |
| console.log("greater than 5: ", gt5); | |
| let lt5 = arr.filter(x => x < 5); | |
| console.log("less than 5: ", lt5); | |
| let div5 = arr.filter(x => x%5 === 0); | |
| console.log("divisible by 5: ", div5); |
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
| arr: [1,2,3,4,5,6,7,8,9,10] | |
| evens: [2,4,6,8,10] | |
| odds: [1,3,5,7,9] | |
| greater than 5: [6,7,8,9,10] l | |
| ess than 5: [1,2,3,4] | |
| divisible by 5: [5,10] |
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
| "use strict"; | |
| var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
| console.log("arr: ", arr); | |
| var even = arr.filter(function (x) { | |
| return x % 2 === 0; | |
| }); | |
| console.log("evens: ", even); | |
| var odd = arr.filter(function (x) { | |
| return x % 2 === 1; | |
| }); | |
| console.log("odds: ", odd); | |
| var gt5 = arr.filter(function (x) { | |
| return x > 5; | |
| }); | |
| console.log("greater than 5: ", gt5); | |
| var lt5 = arr.filter(function (x) { | |
| return x < 5; | |
| }); | |
| console.log("less than 5: ", lt5); | |
| var div5 = arr.filter(function (x) { | |
| return x % 5 === 0; | |
| }); | |
| console.log("divisible by 5: ", div5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment