0

Im working with a library to create tables in react-pdf. And i want to fill it with api data. Is there a way of iterating inside the data{} and create various objects with data from api. Instead of sth like

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

have

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

Code

{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>
...
GBouffard
  • 1,125
  • 4
  • 11
  • 24
Rowan Frank
  • 117
  • 6

1 Answers1

0

you can use Object.entries() to get a 2D array of your object and use something like forEach or map to have loop over it and create the desired data shape:

const finalResult = {}

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

amdev
  • 6,703
  • 6
  • 42
  • 64