I think @InspiredJW did it with ES5, and as @trincot pointed out, using es6 is a better approach. But we can add a bit more sugar, by using the spread operator, and logical AND short circuit evaluation:
const a = {
...(someCondition && {b: 5})
}
More Related Contents:
- How to Get Response From rest URL using jquery
- Cartesian product of multiple arrays in JavaScript
- Pass an extra argument to a callback function
- What is the best way to add options to a select from a JavaScript object with jQuery?
- When a ‘blur’ event occurs, how can I find out which element focus went *to*?
- How do I scroll to an element using JavaScript?
- Does HTML5 allow drag-drop upload of folders or a folder tree?
- JavaScript: using a condition in switch case
- How to check if a number is between two values?
- How to replace selected text with html in a contenteditable element? [duplicate]
- How to use nodejs to open default browser and navigate to a specific URL
- Detect when user accepts to download a file
- How to render HTML string as real HTML?
- Close window automatically after printing dialog closes
- Backbone.js : repopulate or recreate the view?
- How to make a disabled reactive form Editable in Angular2
- Best practices for reducing Garbage Collector activity in Javascript
- How can two instances of a userscript communicate between frames?
- Why is JSHINT complaining that this is a strict violation?
- ES6 – Call static method within a class
- TypeError: [API] is undefined in content script or Why can’t I do this in a content script?
- How to access plain text content retrieved via in JavaScript?
- How can I remove a specific item from an array in JavaScript?
- How to test if a URL string is absolute or relative?
- How to get result of console.trace() as string in javascript with chrome or firefox?
- Can you target an elements parent element using event.target?
- How to load javascript code to an html file at runtime?
- JavaScript: Check if CTRL button was pressed
- Checking if a textbox is empty in Javascript
- Lambda function syntax in JavaScript without curly braces