Skip to main content
added 31 characters in body
Source Link
Nn Karthik
  • 122
  • 3
  • 13

I have a requirement where I have to take a couple of values from a nested object which when not available, should be taken from a default object. For example consider the following object:

const objToDestructure={
   someVal:3,
   anotherVal:4,
   outerObj:{}
}

If I want to take two field field1 and field2 from outerObj then what I was able to do was

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

Is there anyway this can be shortened even more? I tried to do the following:

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

But I got both the values as undefined. Is there any reason why this won't work but the individual assignment does?

I have a requirement where I have to take a couple of values from a nested object which when not available, should be taken from a default object. For example consider the following object:

const objToDestructure={
   someVal:3,
   anotherVal:4,
   outerObj:{}
}

If I want to take two field field1 and field2 from outerObj then what I was able to do was

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

Is there anyway this can be shortened even more? I tried to do the following:

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

But I got both the values as undefined. Is there any reason why this won't work but the individual assignment does?

I have a requirement where I have to take a couple of values from a nested object which when not available, should be taken from a default object. For example consider the following object:

const objToDestructure={
   someVal:3,
   anotherVal:4,
   outerObj:{}
}

If I want to take two field field1 and field2 from outerObj then what I was able to do was

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

Is there anyway this can be shortened even more? I tried to do the following:

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

But I got both the values as undefined. Is there any reason why this won't work but the individual assignment does?

added 31 characters in body
Source Link
Nn Karthik
  • 122
  • 3
  • 13

I have a requirement where I have to take a couple of values from a nested object which when not available, should be taken from a default object. For example consider the following object:

const objToDestructure={
   outerObjsomeVal:{3,
   anotherVal:4,
   outerObj:{}
}

If I want to take two field field1 and field2 from outerObj then what I was able to do was

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

Is there anyway this can be shortened even more? I tried to do the following:

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

But I got both the values as undefined. Is there any reason why this won't work but the individual assignment does?

I have a requirement where I have to take a couple of values from a nested object which when not available, should be taken from a default object. For example consider the following object:

const objToDestructure={
   outerObj:{ 
   }
}

If I want to take two field field1 and field2 from outerObj then what I was able to do was

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

Is there anyway this can be shortened even more? I tried to do the following:

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

But I got both the values as undefined. Is there any reason why this won't work but the individual assignment does?

I have a requirement where I have to take a couple of values from a nested object which when not available, should be taken from a default object. For example consider the following object:

const objToDestructure={
   someVal:3,
   anotherVal:4,
   outerObj:{}
}

If I want to take two field field1 and field2 from outerObj then what I was able to do was

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

Is there anyway this can be shortened even more? I tried to do the following:

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

But I got both the values as undefined. Is there any reason why this won't work but the individual assignment does?

Source Link
Nn Karthik
  • 122
  • 3
  • 13

Destructuring with default objects for nested object

I have a requirement where I have to take a couple of values from a nested object which when not available, should be taken from a default object. For example consider the following object:

const objToDestructure={
   outerObj:{ 
   }
}

If I want to take two field field1 and field2 from outerObj then what I was able to do was

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

Is there anyway this can be shortened even more? I tried to do the following:

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

But I got both the values as undefined. Is there any reason why this won't work but the individual assignment does?