Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React Redux在API响应中重命名嵌套对象_Redux_React Redux - Fatal编程技术网

React Redux在API响应中重命名嵌套对象

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

我从我的API得到一个响应,在我将其发送到reducer之前,我正在尝试重命名操作中对象数组的嵌套对象属性。以下是响应的大致情况:

[
  {
    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
    }
  })
});