0

Following is my code, in which I am trying to add passStatus to an object only if it has a value otherwise omit it.

I tried this one - In Javascript, how to conditionally add a member to an object?

But seems like I am doing it wrong. Any pointers Please.

Code -

var a = {
  firstName: "Tom",
  lastName: "Simmon",
  email: "tomsimmon@gail.com",
  phone: "+36456",
  passStatus: "",
  query: "simple"
};

// var newObj = Object.assign(a, a.passStatus ? {a.passStatus} : null);

var newObj = {
   ...(a.passStatus? {passStatus: a.passStatus}: {} )
}

console.log(newObj); // {} <- Getting a blank object

Expected Output -

If passStatus = ""

{
  firstName: "Tom",
  lastName: "Simmon",
  email: "tomsimmon@gail.com",
  phone: "+36456",
  query: "simple"
}

If passStatus = "pass"

{
  firstName: "Tom",
  lastName: "Simmon",
  email: "tomsimmon@gail.com",
  phone: "+36456",
  passStatus: "pass",
  query: "simple"
}
Nesh
  • 2,389
  • 7
  • 33
  • 54

3 Answers3

4

Destructure the object (a) to remove the value, than use object spread to add it, if it's truthy:

var a = {
  firstName: "Tom",
  lastName: "Simmon",
  email: "tomsimmon@gail.com",
  phone: "+36456",
  query: "simple"
};

const { passStatus, ...obj } = a;

const newObj = {
  ...obj,
  ...(passStatus && { passStatus })
};

console.log(newObj);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
1

I think the clearest way to conditionally remove a property from the object like this would be to use rest syntax followed by a possible Object.assign:

var a = {
  firstName: "Tom",
  lastName: "Simmon",
  email: "tomsimmon@gail.com",
  phone: "+36456",
  passStatus: "",
  query: "simple"
};

const { passStatus, ...newObj } = a;
if (passStatus) {
  Object.assign(newObj, { passStatus });
}

console.log(newObj);

You can do it in one line by destructuring a computed property name, where the property name uses the conditional operator to remove the passStatus property if it exists - but this is hard to understand, I wouldn't recommend it:

var a = {
  firstName: "Tom",
  lastName: "Simmon",
  email: "tomsimmon@gail.com",
  phone: "+36456",
  passStatus: "",
  query: "simple"
};

const { [a.passStatus ? '' : 'passStatus']: _, ...newObj } = a;
console.log(newObj);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • What is the underscore referring to ? here in - `[a.passStatus ? '' : 'passStatus']: _`. Can u please explain this a bit ..this is quite interesting and unique – Nesh Apr 15 '20 at 10:06
  • 1
    The `[]` brackets indicate a computed property name. Eg `['foo' + '1']` will destructure the property `foo1`. The identifier that comes after the `:` is the variable name into which the destructured property gets stored. Here, we don't care about the variable, we just want to remove it so it isn't included in the `...newObj` rest syntax. You could name it anything you want and `console.log` its (empty) value later, if you wanted. But `_` is the convention for a variable name which isn't going to be used. – CertainPerformance Apr 15 '20 at 10:08
1

Shallow copy the entire object, if the new object has a falsey passStatus value, delete the property.

const a = {
  firstName: "Tom",
  lastName: "Simmon",
  email: "tomsimmon@gail.com",
  phone: "+36456",
  passStatus: "",
  query: "simple"
};

const b = {
  firstName: "Tom",
  lastName: "Simmon",
  email: "tomsimmon@gail.com",
  phone: "+36456",
  passStatus: "test",
  query: "simple"
};

const copyKeepOrRemovePassStatus = obj => {
  const newObj = { ...obj };

  if (!newObj.passStatus) {
    delete newObj.passStatus;
  }
  return newObj;
};

console.log(copyKeepOrRemovePassStatus(a));
console.log(copyKeepOrRemovePassStatus(b));
Drew Reese
  • 165,259
  • 14
  • 153
  • 181