Objects are the most important data-type and are building blocks for modern JavaScript. They are different from primitive data-types such as String, Number, Boolean, null, etc. But we can make these datatypes as objects by using the new keyword. There are two approaches to conditionally add a member to an object.
Method 1: This method involves checking if the required condition is true and based on that a property is added to the object.
Example: In this example, if the condition is true then ‘b’ will be added as a member to ‘a’, otherwise not.
Javascript
// Define the object var a = {}; // Check if the condition // is satisfied if (someCondition) { // Add the required // property to the object a.b = 7; } |
Output:
-
When condition is satisfied:
{b: 7} -
When condition is not satisfied:
{}
This approach can also be used in an idiomatic way, using a ternary operator.
Example: In this example, if the condition is true then ‘b’ will be added as a member to ‘a’, otherwise not.
Javascript
var a = { // Use the ternary operator to check // if the property should be added // to the object b: (someCondition? 7 : undefined) }; |
Output:
-
When condition is satisfied:
{b: 7} -
When condition is not satisfied:
{b: undefined}
Method 2: This method is used to add several members to an object. The jQuery library function $.extend() is used for this method. This however does not copy undefined properties to the object.
Example:
Javascript
var newObject = $.extend({}, { a: conditionA ? 7 : undefined, b: conditionB ? 9 : undefined, // More properties as required }); |
Output:
-
When the first condition is satisfied:
{a: 7} -
When both conditions are satisfied:
{a: 7, b: 9}
Recommended Posts:
- How to add a property to a JavaScript object using a variable as the name?
- How to access an object having spaces in the object's key using JavaScript ?
- Is sizeof for a struct equal to the sum of sizeof of each member?
- How to add an object to an array in JavaScript ?
- How to add key/value pair to a JavaScript object?
- JavaScript | Add new attribute to JSON object
- JavaScript | How to add an element to a JSON object?
- How to check a JavaScript Object is a DOM Object ?
- What is the difference between Object.keys() and Object.entries() methods in JavaScript ?
- PHP | Type Casting and Conversion of an Object to an Object of other class
- HTML | DOM Object Object
- How to select first object in object in AngularJS?
- How to add a tooltip to a div using JavaScript?
- How to add tooltip to HTML table cell without using JavaScript ?
- How to add/update an attribute to an HTML element using JavaScript?
- How to add float numbers using JavaScript ?
- How to add filter with Portfolio Gallery using HTML, CSS and JavaScript ?
- How to add fade-out effect using pure JavaScript ?
- How to add fade-in effect using pure JavaScript?
- How to add an element horizontally in Html page using JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


