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 to make Array to Variable

i have some array looks like this:

const [listArr, setLA]= useState([
    {
      id: Math.floor(Math.random * 100000),
      data: "This is data 1",
    },
    {
      id: Math.floor(Math.random * 100000),
      data: "This is data 2",
    },
    ....(and so on)
  ]);

and i need to extract those data to become 1 variable and get it using console looks like

console.log(data);
//This is data 1\nThis is data2

I’ve tried using foreach to put 1 by 1 but still didn’t work, can you guys help me?

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

>Solution :

You may use Array#map function to get an array of the values you want and then join them into a string in order to print to the console:

console.log(listArr.map(({ data }) => data).join('\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