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

Array of object value printing in javascript

I want to print expected output values in two loop using map, Can you please help me.

var data=[{'2021':['Q2 2021','Q3 2021','Q4 2021']},
     {'2022':['Q1 2022','Q2 2022']}
    ]
   
   data.map((obj,ind)=>{
    console.log(Object.keys(obj));
    Object.keys(obj).map((yearQuarter, i) => {
                  console.log(obj[yearQuarter]);
                  })
   })
   //getting output
   //['2022']
   //['Q1 2022', 'Q2 2022']
   //Expected Output
   //'2022'
   //'Q1 2022`
   //'Q2 2022`
   

>Solution :

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

We can use replace to make obj[yearQuarter] print by multiple lines

var data=[{'2021':['Q2 2021','Q3 2021','Q4 2021']},
     {'2022':['Q1 2022','Q2 2022']}
    ]

data.map((obj,ind)=>{
  console.log(Object.keys(obj)[0]);
  Object.keys(obj).map((yearQuarter, i) => {
      console.log(obj[yearQuarter].toString().replace('[]','').replaceAll(',','\n'))
   })
})
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