How to destructure nested object that might be undefined in javascript [duplicate] - javascript

This question already has answers here:
Destructuring with nested objects and default values
(3 answers)
JS/ES6: Destructuring of undefined
(8 answers)
Closed 10 months ago.
I have a function that returns an object like the following:
{
data: {key1: value1, ...},
errors: [...]
}
I can extract key1 with the following:
const { data: { key1 }} = myFunction()
However, sometimes data is undefined which causes the destructuring to fail.
I've looked at the destructuring examples and haven't been able to figure out how to pull out key1 when data might be undefined.
Is there a way to assign a default value of {} to data when performing the destructuring so that it doesn't fail?

You could take a default object.
const
myFunction = () => ({}),
{ data: { key1 } = {} } = myFunction();
console.log(key1);

Related

Destructure property whose name is a reserverd keyword [duplicate]

This question already has answers here:
How to destructure object properties with key names that are invalid variable names?
(3 answers)
Closed 3 years ago.
While using material ui, I realized they have a prop called in in the transitions components, but when trying to destruct the props, I can't because in is a reserved key word.
const MyFade = ({ children, in, ...otherProps }) => { // this gives me an error
return (
<div {...otherProps}>
<Fade in={in}>{children}</Fade>
</div>
);
};
How can I do this? I need to destruct in and have otherProps to spread in the div.
Just assign a new, not reserved name inside destructuring.
const o = {
in: 'foo',
out: 'boo',
};
const { in: inProp } = o;
// ^^^^ assign new name
console.log(inProp);

ES6 - Possible to destructure from object into another object property? [duplicate]

This question already has answers here:
Object destructuring without var, let or const
(4 answers)
Closed 4 years ago.
So I'm trying to figure out if there's any simple ES6 syntax to do the following:
If I have an object
const config = { foo: null, bar: null }
And I want to assign the values of those properties from another object such as:
const source = { hello: "hello", world: "world", another: "lorem", onemore: "ipsum" }
I want to do something like the following but it doesn't work
{ hello:config.foo, world:config.bar } = source
I know I can do something very close like:
{ hello:foo, world:bar } = source
But this creates new variables foo and bar, whereas I want to assign to existing properties on another object. I'm just curious if there's an ES6 shorthand for this; I don't need help doing this with traditional code, I know there are a dozen ways and I already know most of them.
You're just missing brackets () around the statement.
const config = {};
const source = { hello: "hello", world: "world", another: "lorem", onemore: "ipsum" };
({hello: config.foo, world: config.bar} = source);
console.log(config);

Retrieving the variable value to act as a name [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I need an object in Typescript declared like this:
queryParameters = { flagged: true };
Now I would like to have the flagged to be retrieved from a variable name. Something like:
var param = 'flagged';
queryParameters = { ValueOf(param): true };
Any idea ?
Thanks.
Why not use computed property names:
queryParameters = { [param]: true };

Adding a key from a variable string (es6) when using spread syntax [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 6 years ago.
I would like to know if there is a clean way to set the value of a key from a string variable when using spread syntax in es6?
Something like the following:
let keyVar = 'newKey'
let newObject = {keyVar:{some:'json'},...oldObject}
But this leads to:
{"keyVar":{"some":"json"}, ... }
rather than:
{"newKey":{"some":"json"}, ... }
You can use computed properties:
const keyVar = 'newKey';
const newObject = { [keyVar]: { some: 'json' } };
console.log(newObject);

Javascript variable object keys [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 8 years ago.
I am attempting to add a variable key, with no luck.
Here's what I got so far:
mysql('translations',{
check: 'element_id',
element_id: element_id,
{'lang-'+lang_id}: value
});
The variable key is the last line of the function.
Any ideas?
You can't use expressions for the identifiers in an object literal.
First create the object, then you can use dynamic names:
var obj = {
check: 'element_id',
element_id: element_id,
}
obj['lang-'+lang_id] = value;
mysql('translations', obj);
You can do this:
var x = {
check: 'element_id',
element_id: element_id
};
x['lang-'+lang_id] = value;
mysql('translations', x);

Categories