我收到来自 API 的响应,并且在将操作发送到 reducer 之前,我尝试重命名操作中对象数组的嵌套对象属性。响应通常如下所示:
[
{
attributes: {
name: "Item 1",
price_cents: 1500
}
},
{
attributes: {
name: "Item 2",
price_cents: 1000
}
},
...
]
我想将 price_cents
更改为 price
。在将其用作 reducer 的有效负载之前,如何更改它?
最佳答案
您可以使用Array#map
遍历响应数组中的每个值,并使用所需的属性和名称创建一个新对象:
const actionCreator = (response) => ({
type: 'ACTION_TYPE',
payload: response.map((item) => ({
attributes: {
name: item.attributes.name,
price: item.attributes.price_cents
}
})
});
关于redux - React Redux 重命名 API 响应中的嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41914631/