-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: pre-compile "obj{ a, b }" to "(({a,b})->{a,b})(obj)" to create a copy of "obj" with only properties "a" and "b" #5326
Comments
It sounds like you're basically asking for the inverse of object spread, which creates a larger object (i.e. obj1 = a:1, b:2, c:3
obj2 =
a: obj1.a
b: obj1.b It's a bit repetitive, but it avoids extra local variables. Or to get closer to your example, you can create a scope around the destructuring to isolate those variables: obj1 = a:1, b:2, c:3
obj2 = do -> { a, b } = obj1; { a, b }
# obj2 is now {a:1, b:2} |
I've run into this a bunch lately, and find it ineligant that all existing solutions need to repeat A possible extension to destructuring I thought of that I was a bit surprised didn't work is the following: obj1 = {a: 1, b: 2, c: 3}
obj2 = {}
{obj2.a, obj2.b} = obj1 Intended complication: var obj1, obj2;
obj1 = {a: 1, b: 2, c: 3};
obj2 = {};
obj2.a = obj1.a
obj2.b = obj1.b Do you think this would be interesting, or is it confusing notationally? It requires repeating
obj1 = {a: 1, b: 2, c: 3}
obj2 = do -> {a, b} = obj1
|
How about: pick = (obj, keys...) ->
if keys.length is 1 and Array.isArray keys[0]
keys = keys[0]
Object.assign {}, ([key]: obj[key] for key in keys)...
obj1 = a: 1, b: 2, c: 3
obj2 = pick obj1, 'a', 'b' If you don't like the many quotes in qw = (s) -> s.split /\s+/ # like Perl's qw{} operator
obj2 = pick obj1, qw 'a b' I'm not convinced the requested feature is justified. |
https://lodash.com/docs/4.17.15#pick: var object = { 'a': 1, 'b': '2', 'c': 3 };
_.pick(object, ['a', 'c']);
// => { 'a': 1, 'c': 3 } |
Here's another one: #try pick = (cb) -> Reflect.apply cb, res = {}, []; res
obj1 = a:1, b:2, c:3
obj2 = pick ->
{ @a, @b } = obj1 I generally found obj2 = do ->
{ a, b } = obj1
{ a, b } An unfortunate sideeffect of this pattern is that you have to pay attention to variable scope and are conditionally required to shadow your props: a = "something else"
obj2 = do (a, b) -> # ReferenceError: b is not defined
{ a, b } = obj1
{ a, b } |
The expression If it did evaluate to the left side we could just write |
This is in alignment with JS' destructuring assignment and as such will unlikely change. |
It's feature request.
Description:
it's a very common pattern when you need to pickup only some properties from object
and to do this people usually use such constructs:
Input Code
Possible Solution
would be nice to have an "operator" like this:
as we can do
but it destruct local variables "a" and "b"
or
that way is safe but looks ugly )
Thank you!
The text was updated successfully, but these errors were encountered: