javascript - 如何在 React Native 中更改数组键名称

我是 api 响应,我正在获取下面的数组作为响应。我必须更改数组的内部键名称,然后发送到 ui 。请帮忙 。我很困惑。

"callDetails": [
        {
          "quantity":5,
           "msisdn":1,
          "otherMSISDN": 2348032002207
        },
        {
          "quantity": 5,
          "msisdn": 2347062021398,
          "otherMSISDN": 2347038834140
        },
        {
          "quantity": 4,
          "msisdn": 2347062021398,
          "otherMSISDN": 2348166692364
        },
        ]

//我必须将我的数组从上方数组转换为下方数组。

"callDetails": [
        {
          "frquency":5,
           "totalRows":1,
          "frequentNumber": 2348032002207
        },
        {
          "frquency": 5,
          "totalRows": 1,
          "frequentNumber": 2347038834140
        },
        {
          "frquency": 4,
          "totalRows": 1,
          "frequentNumber": 2348166692364
        },
        ]

最佳答案

您可以使用 Array.map()要实现这一点,可以这样做:

const response = {

"callDetails": [
        {
          "quantity":5,
           "msisdn":1,
          "otherMSISDN": 2348032002207
        },
        {
          "quantity": 5,
          "msisdn": 2347062021398,
          "otherMSISDN": 2347038834140
        },
        {
          "quantity": 4,
          "msisdn": 2347062021398,
          "otherMSISDN": 2348166692364
        }
        ]

}

response.callDetails = response.callDetails.map(({quantity, msisdn, otherMSISDN}) => {
  return {
    frquency: quantity,
    totalRows: msisdn,
    frequentNumber: otherMSISDN
  }
});

console.log(response)

https://stackoverflow.com/questions/61590369/

相关文章:

node.js - Yarn workspaces——包别名

reactjs - 是否有任何函数可以在 react-final-form 中获取表单值

c# - 如何使用 Sustainsys.Saml2 设置 NLog

delphi - 如何在 Delphi 10.3 Rio 上安装 Indy?

javascript - 我如何将所选项目从 React Native Paper Menu 传递到

svelte - 如何替换 Svelte 中目标的内容而不是附加到子项?

python - 删除子列表重复项,包括反向

angular - 不同的路线,相同的组件 - 为什么组件不在路线更改时重新加载?

ruby-on-rails - 如何在 Ruby on Rails 6 中将 rich_text_a

amazon-web-services - 通过 HTTPS 提供静态 S3 网站时降低 Cloud