React Redux在API响应中重命名嵌套对象
我从我的API得到一个响应,在我将其发送到reducer之前,我正在尝试重命名操作中对象数组的嵌套对象属性。以下是响应的大致情况:React Redux在API响应中重命名嵌套对象,redux,react-redux,Redux,React Redux,我从我的API得到一个响应,在我将其发送到reducer之前,我正在尝试重命名操作中对象数组的嵌套对象属性。以下是响应的大致情况: [ { attributes: { name: "Item 1", price_cents: 1500 } }, { attributes: { name: "Item 2", price_cents: 1000 } }, ... ] 我想把price\u cent
[
{
attributes: {
name: "Item 1",
price_cents: 1500
}
},
{
attributes: {
name: "Item 2",
price_cents: 1000
}
},
...
]
我想把
price\u cents
改成price
。在将其用作减速机的有效负载之前,我如何更改它?编写一个过滤器来更改属性值,当响应成功返回时,过滤响应并减少它
JSON属性名称更改:
JSON属性名称更改(2):您可以使用来遍历响应数组中的每个值,并使用所需的属性和名称创建一个新对象:
const actionCreator = (response) => ({
type: 'ACTION_TYPE',
payload: response.map((item) => ({
attributes: {
name: item.attributes.name,
price: item.attributes.price_cents
}
})
});