Skip to content

Instantly share code, notes, and snippets.

@meeDamian
Last active August 29, 2015 14:04
Show Gist options
  • Save meeDamian/47006ec066984fa112a9 to your computer and use it in GitHub Desktop.
Save meeDamian/47006ec066984fa112a9 to your computer and use it in GitHub Desktop.
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
// 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