- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Conditionally change object property with JavaScript?
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 }
- Related Articles
- How to change the point size in geom_point conditionally in R?
- Add line break inside a string conditionally in JavaScript
- How to Declare an Object with Computed Property Name in JavaScript?
- Change CSS columns property with Animation
- Change CSS filter property with Animation
- Can we check if a property is in an object with JavaScript?
- Change column-width property with CSS Animations
- Add a property to a JavaScript object constructor?
- Grouping on the basis of object property JavaScript
- Merge object & sum a single property in JavaScript
- Replacing array of object property name in JavaScript
- Removing property from a JSON object in JavaScript
- Change column-rule-width property with CSS Animations
- How do we remove a property from a JavaScript object? - JavaScript
- How to remove a property from a JavaScript object?

Advertisements