0

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?

5
  • What do you mean by one variable? Could you specify more what should be done? Commented Dec 14, 2021 at 6:20
  • Try console.log(listArr.map(({ data }) => data).join('\n')) Commented Dec 14, 2021 at 6:22
  • DUDEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE @HaoWu you are awesome, please put this on answer column Commented Dec 14, 2021 at 6:26
  • Hey @RajeshPaudel thanks for your comment, i'm sorry, i'll try to specify more later.. Commented Dec 14, 2021 at 6:27
  • @HaksatryaBhaswara Sure :) glad it helped Commented Dec 14, 2021 at 6:34

1 Answer 1

1

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'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.