I would like to conditionally add an attribute to an object. Currently I have this:
const obj = {
name: car.name,
battery: car.battery && car.battery,
fuel: car.fuel && car.fuel
}
The problem is that if car.battery
is not present I have battery: undefined
. I would rather prefer not having that attribute in the object if it is not present. How can I achieve that?