0

我正在使用一个库在 react-pdf 中创建表。我想用api数据填充它。有没有办法在数据内部进行迭代{}并使用来自 api.xml 的数据创建各种对象。而不是 sth like

data={[{firstName: "John", lastName: "Smith",country: "Australia"}]},
data={[{firstName: "Josh", lastName: "Pattison",country: "USA"}]}

data={[{firstName: "John", lastName: "Smith",country: "Australia"}],
      [{firstName: "Josh", lastName: "Pattison",country: "USA"}]
}

代码

{Data.attributes.map((details) => (
  <TableBody data={[
      {firstName:  details.attributes.filter(
        (x) => x.displayName === "first name"
      )[0].value, 
      lastName: details.attributes.filter(
        (x) => x.displayName === "last name"
      )[0].value, 
      country: details.attributes.filter(
        (x) => x.displayName === "country"
      )[0].value},
  
  ]}>
</TableBody>
...
4

1 回答 1

0

您可以使用Object.entries()获取对象的 2D 数组并使用类似forEach或 map 之类的东西对其进行循环并创建所需的数据形状:

const finalResult = {}

Object.entries(apiData).map(([key, value]) => {
   // do what ever you want here and fill your desired structure
})

于 2020-08-04T07:36:48.157 回答