If you can use ES6, use the spread properties.
function create(includeB) {
return {
a : 1,
...(includeB ? { b: 2 } : {}),
};
}
More Related Contents:
- How to get all the values associated with an object in array?
- How to remove all duplicates from an array of objects?
- How to get a subset of a javascript object’s properties
- Creating object with dynamic keys [duplicate]
- How do I create a dynamic key to be added to a JavaScript object variable [duplicate]
- Sort array of objects by single key with date value
- Is it possible to get the non-enumerable inherited property names of an object?
- How do you check if a JavaScript Object is a DOM Object?
- How does the new operator work in JavaScript?
- How to add an object to an array
- Iterate through object properties
- How to display all methods of an object?
- Clone Object without reference javascript [duplicate]
- Why Array.indexOf doesn’t find identical looking objects
- Which way is best for creating an object in JavaScript? Is `var` necessary before an object property?
- Why in JavaScript both “Object instanceof Function” and “Function instanceof Object” return true?
- JSON.stringify function
- In javascript how can I dynamically get a nested property of an object
- Can a JavaScript object property refer to another property of the same object? [duplicate]
- How do you create a method for a custom object in JavaScript?
- Uncaught TypeError: Object.values is not a function JavaScript
- How to create Javascript constants as properties of objects using const keyword?
- Multiple arguments vs. options object
- How do I count a JavaScript object’s attributes? [duplicate]
- Get the property of the difference between two objects in javascript
- Object referencing its own property on initialization [duplicate]
- Converting Array of Objects into Array of Arrays
- Using non-ASCII character as JavaScript object key [duplicate]
- Difference between ( for… in ) and ( for… of ) statements?
- Find object having maximum value for the `id` property in an array of objects