Skip to content

Instantly share code, notes, and snippets.

@jineeshjohn
Last active December 2, 2016 13:40
Show Gist options
  • Save jineeshjohn/9f72286bec4ac76316ade73a2541d5a5 to your computer and use it in GitHub Desktop.
Save jineeshjohn/9f72286bec4ac76316ade73a2541d5a5 to your computer and use it in GitHub Desktop.
Single level of abstraction - SLA
var responseJson = [
{type: 'input', id: 'key1', styles:{display:'block', color: 'red'}, someprop:'abc', someotherprop:'bcd'},
{type: 'radio', id: 'key2', styles:{display:'inline', color: 'blue'}, someprop:'xyz', someotherprop:'z'}
]
var parseResponse = function(response) {
var result = [];
for (var i=0; i<response.length; i++) {
var resultObj = {
type: response[i].type,
id: response[i].id,
styles: response[i].styles,
someprop: response[i].someprop
}
result.push(resultObj);
}
return result;
}
var parseResponse2 = function(response) {
return _.map(response, function(item){ // Iteration
return _.pick(item, 'type', 'id', 'styles', 'someprop'); // picking the relevent properties
});
}
var result = parseResponse(responseJson);
var result = parseResponse2(responseJson);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment