Given an Object s
as
s = {
a:1,
b:2
}
I can define a new object t
with fields depending on the contents of s
,
say something like
t = {
d: s.a ? 1 : 2
}
then the field d
value depends on s.a ( Value of d depends on whether s.a
is defined or not).
How can I exclude a field in an object depending o the value of s? , Something like
t = {
d: s.a ? 1 : undefined
}
This doesn't work though ...
I know this can be done with a couple of if
else
but I'm looking for an elegant solution/ oneliners
My object is pretty huge, so I do not want to do something like
t = s.a? {
d: 1
} : {}
EDIT
I've seen a wide variety of solutions, I'm looking at a solution that is a oneliner/has minimum changes and is readable. Something like an idiomatic javascript
/ecmascript 6
EDIT The duplicate found here seems to be for javascript and does give correct answers to this question. I'm hoping the new ecmascript might have a newer solution to this question