简体   繁体   English

从 object 中提取数组?

[英]Extract array from object?

I have a component who receive props:我有一个接收道具的组件:

The data recived printed on console.log收到的数据打印在 console.log 上

在此处输入图像描述

How to extract the array from this object?如何从此 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.我终于用 function map() 解决了这个问题。

It sounds like you have a React component that is receive data like this:听起来您有一个接收数据的 React 组件,如下所示:

<MyComponent data={myArray} />

If so, then inside that component, it will receive it on the prop named data .如果是这样,那么在该组件内部,它将在名为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>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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