Better way to restrict object to certain attributes
Suppose I have an object:
obj =
foo: 'foo'
bar: 'bar'
... and lots of other stuff ...
... these values vary, and are not actually hard coded ...
I'd like to create a copy of that object which contains only a subset of
its attributes.
obj2 =
foo: obj.foo
bar: obj.bar
// *doesn't* copy any other attributes from `obj`
Is there a more idiomatic way of doing this? Something like
obj2 = copy_only obj, ['foo', 'bar']
I mean, sure, I could write copy_only myself, but I'm looking for the
idiomatic way to do this, not the home baked way. Is there such an idiom
that you can recommend for this scenario?
No comments:
Post a Comment