Linked Questions

122 votes
4 answers
75k views

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: ... }...
Andrew Mao's user avatar
13 votes
1 answer
14k views

Here's my code: app.post('/ujfeladat', (req, res) => { const {nev, tipus, szid} = req.body; const hianyos = () => { if(tipus === 'hianyos'){ return {rang: -...
user avatar
0 votes
6 answers
12k views

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....
mbesson's user avatar
  • 749
0 votes
1 answer
4k views

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 ...
B''H Bi'ezras -- Boruch Hashem's user avatar
1 vote
2 answers
2k views

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 ...
Osakr's user avatar
  • 1,066
0 votes
2 answers
3k views

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: { ...
Antonio Ortells's user avatar
1 vote
5 answers
1k views

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, : {}; (...
GladH's user avatar
  • 25
1 vote
1 answer
2k views

Let’s consider the next array: const arr = [ { name: "bob", age: 25, salary: 1000 }, { name: "bill", age: 32, salary: 1500 }, { name: "jake", ...
anotheruser's user avatar
0 votes
1 answer
1k views

I need to update Existing object conditionally using spread operator. Below my code const resetPerson: any = {...person, name: '', age: '', location && location : '' }; if location key ...
Nagasai's user avatar
  • 11
2 votes
0 answers
1k views

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 ...
simonzack's user avatar
  • 21.2k
0 votes
1 answer
1k views

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(), "...
Joshua's user avatar
  • 95
-1 votes
1 answer
319 views

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 &&...
andreaspfr's user avatar
  • 2,314
3 votes
1 answer
141 views

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 ...
Seph Reed's user avatar
  • 11.5k
-2 votes
3 answers
443 views

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 ...
mr.incredible's user avatar
0 votes
0 answers
304 views

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 ...
James's user avatar
  • 461

15 30 50 per page