Last active
August 29, 2015 14:04
-
-
Save meeDamian/47006ec066984fa112a9 to your computer and use it in GitHub Desktop.
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
| A = (a) -> | |
| a | |
| .filter (b) -> b.type is 'email' | |
| .map (b) -> b.value | |
| B = (a) -> | |
| b.value for b in a when b.type is 'email' | |
| array = [ | |
| type: 'email' | |
| value: '[email protected]' | |
| , | |
| type: 'email' | |
| value: '[email protected]' | |
| , | |
| type: 'phone' | |
| value: '1234567890' | |
| , | |
| type: 'email' | |
| value: '[email protected]' | |
| , | |
| type: 'email' | |
| value: '[email protected]' | |
| ] | |
| console.log A array | |
| console.log B 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
| // reusable helpers | |
| var prop = function(name) { | |
| return function(o) { | |
| return o[name]; | |
| } | |
| }; | |
| var by = function(prop, value) { | |
| return function(o) { | |
| return o[prop] === value; | |
| }; | |
| }; | |
| // actual code | |
| var A = function(a) { | |
| return a | |
| .filter(by('type', 'email')) | |
| .map(prop('value')); | |
| }; | |
| var B = function(a) { | |
| var _ret = []; | |
| a.forEach(function(b){ | |
| if(b['type']==='email') _ret.push(b.value); | |
| }); | |
| return _ret; | |
| }; | |
| var array = [{ | |
| 'type': 'email', | |
| value: '[email protected]' | |
| }, { | |
| 'type': 'email', | |
| value: '[email protected]' | |
| }, { | |
| 'type': 'phone', | |
| value: '1234567890' | |
| }, { | |
| 'type': 'email', | |
| value: '[email protected]' | |
| }, { | |
| 'type': 'email', | |
| value: '[email protected]' | |
| }]; | |
| console.log(A(array)); | |
| console.log(B(array)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment