Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I perform destructuring in the following code properly

I was learning destructuring and got stuck when both the key and value of the object are strings.

This is the App.js and here I am destructuring the object, I am getting a syntax error.

I have commented the line where I am getting an error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import "./styles.css";
const info = {
  name: "Ankit Singh Chauhan", 
  standard : 12 , 
  subjects : {
    sub1 : "Maths" , 
    sub2 : "Science" , 
    sub3 :{
      "sub3.1" : "ballleballe" , 
      "sub3.2" : "AdvancePhysics",
    }
  }
}
export default function App() {
  const {name ,standard ,subjects} = info ;
  const {sub1 , sub2 , sub3} =  subjects;
  // const { "sub3.1" , "sub3.2"} = sub3 ; 
  return (
      <>
      <div>
        <h3> Hi my name is {name} and I study in class {standard} my favourite subjects are 
           {sub1} and {sub2}</h3>  
        <h1> And the advance one are {sub3["sub3.1"]} and {sub3.["sub3.2"]}  </h1>     
      </div>
      </> 
  );
}

Can you suggest me some edits?

>Solution :

You can still destructure the keys but you have to rename them into a valid identifier:

const { "sub3.1": sub3dot1, "sub3.2": sub3dot2 } = sub3;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading