简体   繁体   中英

Extract array from object?

I have a component who receive props:

The data recived printed on console.log

在此处输入图像描述

How to extract the array from this object?

Before I send the array to my component look like this:

在此处输入图像描述

If you are doing

console.log(props);

you can get the array with:

const arr = props.data;

or

const { data } = props;

I have finally used the function map() to resolve this issue.

It sounds like you have a React component that is receive data like this:

<MyComponent data={myArray} />

If so, then inside that component, it will receive it on the prop named data .

function MyComponent(props) {
  console.log(props.data)
  return <p>{data}</p>
}

Or written with the more common destructuring assignment:

function MyComponent({data}) {
  console.log(data)
  return <p>{data}</p>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM