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: "[email protected]",
  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: "[email protected]",
  phone: "+36456",
  query: "simple"
}

If passStatus = "pass"

{
  firstName: "Tom",
  lastName: "Simmon",
  email: "[email protected]",
  phone: "+36456",
  passStatus: "pass",
  query: "simple"
}

3 Answers 3

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: "[email protected]",
  phone: "+36456",
  query: "simple"
};

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

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

console.log(newObj);

Sign up to request clarification or add additional context in comments.

Comments

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: "[email protected]",
  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: "[email protected]",
  phone: "+36456",
  passStatus: "",
  query: "simple"
};

const { [a.passStatus ? '' : 'passStatus']: _, ...newObj } = a;
console.log(newObj);

2 Comments

What is the underscore referring to ? here in - [a.passStatus ? '' : 'passStatus']: _. Can u please explain this a bit ..this is quite interesting and unique
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.
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: "[email protected]",
  phone: "+36456",
  passStatus: "",
  query: "simple"
};

const b = {
  firstName: "Tom",
  lastName: "Simmon",
  email: "[email protected]",
  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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.