Linked Questions
56 questions linked to/from How to conditionally add a member to an object?
122
votes
4
answers
75k
views
Succinct/concise syntax for 'optional' object keys in ES6/ES7? [duplicate]
There are already a lot of cool features in ES6/ES7 for defining Javascript objects. However, the following pattern is common in Javascript:
const obj = {
requiredKey1: ...,
requiredKey2: ...
}...
13
votes
1
answer
14k
views
Assign, and set js object property conditionally [duplicate]
Here's my code:
app.post('/ujfeladat', (req, res) => {
const {nev, tipus, szid} = req.body;
const hianyos = () => {
if(tipus === 'hianyos'){
return {rang: -...
0
votes
6
answers
12k
views
How to conditionally create object property in one line? [duplicate]
I want to create a property on an object conditionally.
The idea is to ensure the property doesn't exist (so not just null) if it has no value.
What I'm doing right now:
// comes from users
req....
0
votes
1
answer
4k
views
JavaScript insert a new property to inline object only if condition is met [duplicate]
I'm aware with ES6 JavaScript object you can dynamically declare variables as object keys, using [], for example:
{[2 + 2]: "four!"}
gives the output {"4": "four!"}
the question is, can a similar ...
1
vote
2
answers
2k
views
Add or avoid adding a property in an object depending on a boolean within the object itself [duplicate]
Basically I wonder if I could avoid adding a property into an object if a variable is false but from inside the object. So let's say I have this object:
var obj = { foo: 'bar', bar: 'foo' };
Now I ...
0
votes
2
answers
3k
views
Create JS object with optional attributes, depending on them being defined [duplicate]
I have a function that will call an external resource (e.g. REST) and will return a JSON object depending on the result.
For instance if I send a POST and it works, I need the object to be:
{
...
1
vote
5
answers
1k
views
How can I put a ternary conditional in a JS object literal [duplicate]
How can I put a ternary conditional in a JS object literal?
var options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
var overlayEXVOORh = {
(options[0] == 1) ? "01" : EXVOORh01, : {};
(...
1
vote
1
answer
2k
views
Map object property only when it is defined (not null) [duplicate]
Let’s consider the next array:
const arr = [
{
name: "bob",
age: 25,
salary: 1000
},
{
name: "bill",
age: 32,
salary: 1500
},
{
name: "jake",
...
0
votes
1
answer
1k
views
How to update Object key value conditionally using spread operator? [duplicate]
I need to update Existing object conditionally using spread operator.
Below my code
const resetPerson: any = {...person, name: '', age: '', location && location : '' };
if location key ...
2
votes
0
answers
1k
views
Creating a javascript object with an optional key [duplicate]
I'm looking for an idiomatic method of creating an object with an optional key, i.e. the shortest way of doing the following:
let obj = {a: 1}
if(condition)
obj['b'] = 2
console.log(obj)
One way ...
0
votes
1
answer
1k
views
How to set a variable in an JSON array only when it is not null [duplicate]
What's the best way to set a variable in a JSON array only when a value is not null and is not undefined?
Example code is:
var test = {
"email": object.email.toString(),
"...
-1
votes
1
answer
319
views
How to conditionally add attribute to an object? [duplicate]
I would like to conditionally add an attribute to an object. Currently I have this:
const obj = {
name: car.name,
battery: car.battery && car.battery,
fuel: car.fuel &&...
3
votes
1
answer
141
views
Is there a shorter syntax for conditionally setting properties in an object? [duplicate]
I'm trying to achieve a solution to this problem that has both form and function. Below are two examples of the problem, one is dysfunctional but has the level of form I'd like to achieve, and the ...
-2
votes
3
answers
443
views
How to optionally initialize object property [duplicate]
Is there any approach in JS to initialize a property of an object conditionally? I can't find any information about this yet.
Example: there is a variable, and if it is not null or undefined, then ...
0
votes
0
answers
304
views
How to include and exclude property in javascript conditionaly? [duplicate]
I have below javascript object and wanted to some property based on conditionally. If the type is not match then don't include those property in object. Don't wanted to set null or undefined ...