0

Basically I wonder if I could avoid adding a property into an object if a variable is false but from inside the object. So let's say I have this object:

var obj = { foo: 'bar', bar: 'foo' };

Now I want to rewrite the same object but I only want to add the second property if a new variable, which is a boolean, is true. The problem is how could I do this for example with a ternary operator like this:

var add = false;
var obj = {
  foo: 'bar',
  (add ? bar: 'foo': null)
};

What I want to avoid is to have this:

...
bar: ( add ? 'foo' : undefined )

Because I don't want to have the bar index in case add == false. Also the assignament must be inside to object ( that's the question about, if it's posible ) because something like this is not what I'm looking for:

...
if (!add) delete obj.bar; // This would be after the creation of the whole object

Work arounds

Obviously this can be achieved in many ways, but I haven't found any that is done inside the object itself. I could use a ternary operator and having two objects like this:

var obj = add ? {
  foo: 'bar',
  bar: 'foo'
} : { foo: 'bar' };

But this would lead to having duplicated code ( when the object has more properties ).

Edit I'd say that my question is slightly different from the duplicates since my question refers to do it inside the object not after its declaration nor anything that isn't between var obj = { ... } There is an accepted answer but I would love if someone knows any other way of doing it without the use of spread operator. Thank you all

Osakr
  • 1,027
  • 1
  • 13
  • 26
  • So, if it isn't there, it should return `null`? – Tyler Roper Jan 11 '19 at 16:36
  • @TylerRoper no, it simply shouldn't be there, like if you delete the property. – Osakr Jan 11 '19 at 16:37
  • What is wrong with `var obj = { foo: 'bar', bar: (add ? 'foo' : null) };`? – Bergi Jan 11 '19 at 16:39
  • There is nothing wrong with it ( it terms of syntax ) but I want to avoid having the `bar` index. Because when I send the data if the `bar` index is present then it fails – Osakr Jan 11 '19 at 16:41
  • 1
    Another duplicate that has slightly more discussion: https://stackoverflow.com/questions/11704267/in-javascript-how-to-conditionally-add-a-member-to-an-object – Tyler Roper Jan 11 '19 at 16:42
  • 1
    @Bergi that post doens't solve my question. It's a different question.... – Osakr Jan 11 '19 at 16:43
  • @Osakr Bergi's answer to the marked duplicate is literally the exact same as the answer below by bugs. It definitely answers your question. – Tyler Roper Jan 11 '19 at 16:43
  • Well, I can't use spread operator in my project :( – Osakr Jan 11 '19 at 16:44
  • 2
    @Osakr You should have mentioned that, as it really reduces your options. The duplicate I commented a bit ago may have more alternatives: https://stackoverflow.com/questions/11704267/in-javascript-how-to-conditionally-add-a-member-to-an-object - Not to mention, the same answer that bergi provided in the marked duplicate also includes an alternative *without* the spread operator, using `Object.assign`. – Tyler Roper Jan 11 '19 at 16:45
  • @TylerRoper you're right. I would have done it if i would knew that this could be done with spread operator :D – Osakr Jan 11 '19 at 16:47
  • At least I'm happy that nobody downvoted the post :) – Osakr Jan 11 '19 at 17:04

2 Answers2

3

A quick way of doing it using the spread operator:

const condition = false;

const foo = {
  bar: 'baz',
  ...(condition ? {
    boz: 'bat'
  } : {})
};

console.log(foo);

This works because the spread operator for object literals does nothing on objects without enumerable own properties.

bugs
  • 14,631
  • 5
  • 48
  • 52
1

To achieve expected result , use below option of adding propeter based on condition in separate line

https://codepen.io/nagasai/pen/ebQmrb?editors=1010

var add = false;
var obj = { foo: 'bar'};
if(add){
  obj.bar = 'foo'
}

console.log(obj)

Option 2: Use undefined value for bar, if condition is false and use JSON.stringify and JSON.parse to remove undefined 'bar' property on condition -false

var add = false;
var obj = JSON.parse(JSON.stringify({ "foo": 'bar', "bar" : add? 'foo': undefined}));
console.log(obj);

codepen - https://codepen.io/nagasai/pen/zyMgxj?editors=1010

Naga Sai A
  • 10,771
  • 1
  • 21
  • 40