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
1 Answer
Reset to default 8You 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
版权声明:本文标题:javascript - Add property to array of objects React Native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744808017a2626267.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论