You could use Object.assign
in combination with the ternary operator:
let data = Object.assign({},
first === null ? null : {first},
...
);
This works because Object.assign
will skip over null
parameters.
If you are sure that the property value is not going to be "falsy", then it would be bit shorter to write:
let data = Object.assign({},
first && {first},
...
);
Assuming the object is going to be stringified at some point, since stringification ignores undefined values, you could also try
let data = {
first: first === null ? undefined : first,
...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…