Skip to main content
deleted 140 characters in body
Source Link
Ori Drori
  • 195.8k
  • 32
  • 244
  • 235

Because outerObj exists (it's not undefined) in objToDestructure, setting a default for it won't help in this case. Since it's an object, you can destructure field1 and field2 individually, and set a default for each of them:

Set a default empty object, and add a default value for each field:

const { outerObj:{ field1 = 22, field2 = 33} = {}} = objToDestructure;

Example:

const { outerObj: objToDestructure={ 
 field1 = 22someVal:3, field2 
 = 33} =anotherVal:4,
 {}} = outerObj:{}
}
console.log(field1, field2)

And you can also set a default for the outerObj (as you tried before):

 
const { outerObj: { field1, field2} = { field1:22, field2:33 }= 33} = {}} = objToDestructure

console.log(field1, field2)

Note: the difference between 1 and 2 is that if outerObj exists, the default in 2 won't be used. The defaults in 1 would be used as long as the individual properties don't exist.

Set a default empty object, and add a default value for each field:

const { outerObj:{ field1 = 22, field2 = 33} = {}} = objToDestructure;

Example:

const { outerObj: { field1 = 22, field2 = 33} = {}} = {}

console.log(field1, field2)

And you can also set a default for the outerObj (as you tried before):

const { outerObj: { field1, field2} = { field1:22, field2:33 } } = {}

console.log(field1, field2)

Note: the difference between 1 and 2 is that if outerObj exists, the default in 2 won't be used. The defaults in 1 would be used as long as the individual properties don't exist.

Because outerObj exists (it's not undefined) in objToDestructure, setting a default for it won't help in this case. Since it's an object, you can destructure field1 and field2 individually, and set a default for each of them:

Set a default empty object, and add a default value for each field:

const { outerObj:{ field1 = 22, field2 = 33} = {}} = objToDestructure;

Example:

const objToDestructure={ 
   someVal:3, 
   anotherVal:4,
   outerObj:{}
}
 
const { outerObj: { field1 = 22, field2 = 33} = {}} = objToDestructure

console.log(field1, field2)

deleted 143 characters in body
Source Link
Ori Drori
  • 195.8k
  • 32
  • 244
  • 235

Set a default empty object, and add a default value for each field:

const { outerObj:{ field1 = 22, field2 = 33} = {}} = objToDestructure;

Example:

const { outerObj: { field1 = 22, field2 = 33} = {}} = {}

console.log(field1, field2)

And you can also set a default for the outerObj (as you tried before):

<!--const begin{ snippetouterObj: js{ hide:field1, falsefield2} console:= true{ babelfield1:22, falsefield2:33 -->} } = {}

console.log(field1, field2)

  

Note: the difference between 1 and 2 is that if outerObj exists, the default in 2 won't be used. The defaults in 1 would be used as long as the individual properties don't exist.

Set a default empty object, and add a default value for each field:

const { outerObj:{ field1 = 22, field2 = 33} = {}} = objToDestructure;

Example:

const { outerObj: { field1 = 22, field2 = 33} = {}} = {}

console.log(field1, field2)

And you can also set a default for the outerObj (as you tried before):

<!-- begin snippet: js hide: false console: true babel: false -->

 

Note: the difference between 1 and 2 is that if outerObj exists, the default in 2 won't be used. The defaults in 1 would be used as long as the individual properties don't exist.

Set a default empty object, and add a default value for each field:

const { outerObj:{ field1 = 22, field2 = 33} = {}} = objToDestructure;

Example:

const { outerObj: { field1 = 22, field2 = 33} = {}} = {}

console.log(field1, field2)

And you can also set a default for the outerObj (as you tried before):

const { outerObj: { field1, field2} = { field1:22, field2:33 } } = {}

console.log(field1, field2)

 

Note: the difference between 1 and 2 is that if outerObj exists, the default in 2 won't be used. The defaults in 1 would be used as long as the individual properties don't exist.

added 638 characters in body
Source Link
Ori Drori
  • 195.8k
  • 32
  • 244
  • 235

Set a default empty object, and add a default value for each field:

const { outerObj:{ field1 = 22, field2 = 33} = {}} = objToDestructure;

Example:

const { outerObj: { field1 = 22, field2 = 33} = {}} = {}

console.log(field1, field2)

And you can also set a default for the outerObj (as you tried before):

<!-- begin snippet: js hide: false console: true babel: false -->

Note: the difference between 1 and 2 is that if outerObj exists, the default in 2 won't be used. The defaults in 1 would be used as long as the individual properties don't exist.

Set a default empty object, and add a default value for each field:

const { outerObj:{ field1 = 22, field2 = 33} = {}} = objToDestructure;

Example:

const { outerObj: { field1 = 22, field2 = 33} = {}} = {}

console.log(field1, field2)

Set a default empty object, and add a default value for each field:

const { outerObj:{ field1 = 22, field2 = 33} = {}} = objToDestructure;

Example:

const { outerObj: { field1 = 22, field2 = 33} = {}} = {}

console.log(field1, field2)

And you can also set a default for the outerObj (as you tried before):

<!-- begin snippet: js hide: false console: true babel: false -->

Note: the difference between 1 and 2 is that if outerObj exists, the default in 2 won't be used. The defaults in 1 would be used as long as the individual properties don't exist.

Source Link
Ori Drori
  • 195.8k
  • 32
  • 244
  • 235
Loading