Tutorial by Examples

You want to convert all elements in an array to some other form. For example, you have theUsers = [ {id: 1, username: 'john'} {id: 2, username: 'lexy'} {id: 3, username: 'pete'} ] and you want to have an array of usernames only, i.e. ['john', 'lexy', 'pete'] Method 1 - using .map ...
theUsers = [ {id: 1, username: 'john'} {id: 2, username: 'lexy'} {id: 3, username: 'pete'} ] To retain only users whose id is greather than 2, use the following: [{id: 3, username: 'pete'}] Method 1 - using .filter filteredUsers = theUsers.filter (user) -> user.id >= 2 Met...
If you want to extract a subset of an array (i.e. numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]) you can easily do this with one of the following examples: numbers[0..2] will return [1, 2, 3] numbers[3...-2] will return [3, 4, 5, 6] numbers[-2..] will return [8, 9] numbers[..] will return [1, 2, 3, 4,...
You want to combine arrays into one. For example, you have fruits = ['Broccoli', 'Carrots'] spices = ['Thyme', 'Cinnamon'] and you want to combine them into ingredients = ['Broccoli', 'Carrots', 'Thyme', 'Cinnamon'] Method 1 - using .concat ingredients = fruits.concat spices Method 2 -...
You can do neat things via the results of Array "comprehensions"... Like assign multiple variables... from the result of a looping for statement... [express,_] = (require x for x in ['express','underscore']) Or a syntactically sweet version of a "mapped" function call, etc.....

Page 1 of 1