Day #10 Pro Tip #Javascript
Conditionally set an object property using the spread operator !!
Published in
1 min readApr 11, 2018
Let’s consider this example:
var object = {
"name": "Arwa",
"city": "Mumbai"
}
In the above object, we want to add publications property only if the writer value is true, this can be achieved in the following ways
const writer = truevar object = {
"name": "Arwa",
"city": "Mumbai",
...writer ? {"publications": "Geekabyte"} : {}
}
OR
const writer = truevar object = {
"name": "Arwa",
"city": "Mumbai",
...writer && {"publications" : "Geekabyte"}
}
I found this really amazing !!
Thanks for reading
Stay tuned for more updates !!!