War es hilfreich?

Frage

Conditionally change object property with JavaScript?

JavascriptWeb DevelopmentObject Oriented Programming

To conditionally change object property, use the logical AND operator ( &&).

If both the operands are non-zero, then the condition becomes true in JavaScript’s logical AND operator.

Example

let marksDetails = { Mark1: 33, Mark2: 89 },isBacklog = false;
console.log("Result when backlog is set to false===");
console.log({ ...marksDetails, ...isBacklog === true && { Mark1: 33 }});
isBacklog = true;
console.log("Result when backlog is set to true===");
console.log({ ...marksDetails, ...isBacklog === true && { Mark1: 93 }});

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo77.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo77.js
Result when backlog is set to false===
{ Mark1: 33, Mark2: 89 }
Result when backlog is set to true===
{ Mark1: 93, Mark2: 89 }
raja
Published on 07-Sep-2020 11:29:35
Advertisements
War es hilfreich?
Nicht verbunden mit Tutorialspoint
scroll top