Functional JavaScript, remove assignment -
how can avoid self
variable here?
function urlbuilder(endpoint){ var status = ["user_timeline", "home_timeline", "retweets_of_me", "show", "retweets", "update", "update_with_media", "retweet", "unretweet", "retweeters", "lookup" ], friendships = ["incoming", "outgoing", "create", "destroy", "update", "show"]; let endpoints = { status: status, friendships: friendships } var self = { }; endpoints[endpoint].foreach(e => { self[e] = endpoint + "/" + e; }); return self; }
somewhat better, still assignment statement.
return [{}].map(el => { endpoints[endpoint].foreach(e => { el[e] = endpoint + "/" + e; }); return el; })[0];
you cannot really. create object dynamic number of properties (and without using json.parse
) need mutate assignment or similar.
your best bet might be
return endpoints[endpoint].reduce((self, e) => { self[e] = endpoint + "/" + e; return self; }, {});
which quite functional.
Comments
Post a Comment