Haksatrya Bhaswara
Haksatrya Bhaswara

Reputation: 237

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?

Upvotes: 0

Views: 43

Answers (1)

Hao Wu
Hao Wu

Reputation: 20699

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

Upvotes: 1

Related Questions