Home > OS >  How to make Array to Variable
How to make Array to Variable

Time:12-14

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?

CodePudding user response:

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'))
  • Related