admin管理员组

文章数量:1404554

I use a library that calls an API which returns an array of objects:

new Promise((resolve) => {
  Kit.getSamples(walkingSample, (err, results) => {
    if (err) {
      return resolve([])
    }
    this.setState({
      ActivityItem: results
    })
  })
})

where ActivityItem is a an empty array defined in state. Now, this will return something like:

[{
    "end": "2020-04-25T21:45:00.000+0200",
    "quantity": 11,
    "sourceName": "Health",
    "start": "2020-04-25T21:45:00.000+0200",
    "tracked": false
  },
  {
    "end": "2020-04-25T21:41:00.000+0200",
    "quantity": 21,
    "sourceName": "Health",
    "start": "2020-04-25T21:41:00.000+0200",
    "tracked": false
  }
]

This is what an API returns, and this is what is stored inside my ActivityItem array. But apart from what API returns, I need to add one more property like 'name' to distinguish different future calls. I wonder if there is any way to add some property to that array so that it will look like this:

[{
    "name": 'Walking',
    end ": "
    2020 - 04 - 25 T21: 45: 00.000 + 0200 ", "
    quantity ": 11, "
    sourceName ": "
    Health ", "
    start ": "
    2020 - 04 - 25 T21: 45: 00.000 + 0200 ", "
    tracked ": false}, {
      "name": 'SomeOther',
      "end": "2020-04-25T21:41:00.000+0200",
      "quantity": 21,
      "sourceName": "Health",
      "start": "2020-04-25T21:41:00.000+0200",
      "tracked": false
    }]

I use a library that calls an API which returns an array of objects:

new Promise((resolve) => {
  Kit.getSamples(walkingSample, (err, results) => {
    if (err) {
      return resolve([])
    }
    this.setState({
      ActivityItem: results
    })
  })
})

where ActivityItem is a an empty array defined in state. Now, this will return something like:

[{
    "end": "2020-04-25T21:45:00.000+0200",
    "quantity": 11,
    "sourceName": "Health",
    "start": "2020-04-25T21:45:00.000+0200",
    "tracked": false
  },
  {
    "end": "2020-04-25T21:41:00.000+0200",
    "quantity": 21,
    "sourceName": "Health",
    "start": "2020-04-25T21:41:00.000+0200",
    "tracked": false
  }
]

This is what an API returns, and this is what is stored inside my ActivityItem array. But apart from what API returns, I need to add one more property like 'name' to distinguish different future calls. I wonder if there is any way to add some property to that array so that it will look like this:

[{
    "name": 'Walking',
    end ": "
    2020 - 04 - 25 T21: 45: 00.000 + 0200 ", "
    quantity ": 11, "
    sourceName ": "
    Health ", "
    start ": "
    2020 - 04 - 25 T21: 45: 00.000 + 0200 ", "
    tracked ": false}, {
      "name": 'SomeOther',
      "end": "2020-04-25T21:41:00.000+0200",
      "quantity": 21,
      "sourceName": "Health",
      "start": "2020-04-25T21:41:00.000+0200",
      "tracked": false
    }]
Share edited Apr 26, 2020 at 16:33 Ardy Febriansyah 7681 gold badge6 silver badges15 bronze badges asked Apr 26, 2020 at 13:31 Vlad DemyanVlad Demyan 3632 gold badges5 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You can use map to generate a new array and add a property to each object:

const newData = data.map(item => return { ...item, name: 'Walking });

本文标签: javascriptAdd property to array of objects React NativeStack Overflow