【问题标题】:how to change array key name in react native如何在本机反应中更改数组键名
【发布时间】:2020-08-18 18:28:10
【问题描述】:

我是 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
        },
        ]

【问题讨论】:

    标签: javascript arrays reactjs react-native


    【解决方案1】:

    您可以使用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)

    【讨论】:

    • 谢谢大家
    【解决方案2】:

    const oldArray = [
            {
              "quantity": 5,
              "msisdn": 1,
              "otherMSISDN": 2348032002207
            },
            {
              "quantity": 5,
              "msisdn": 2347062021398,
              "otherMSISDN": 2347038834140
            },
            {
              "quantity": 4,
              "msisdn": 2347062021398,
              "otherMSISDN": 2348166692364
            },
    ];
    
    const newArray = oldArray.map(
      ({ quantity, msisdn, otherMSISDN }) => ({
        frquency: quantity,
        totalRows: msisdn,
        frequentNumber: otherMSISDN
      })
    );
    
    console.log(newArray);

    【讨论】:

      【解决方案3】:

      您可以使用map 并返回一个带有新键名对象的数组

      var callDetails = [{
          "quantity": 5,
          "msisdn": 1,
          "otherMSISDN": 2348032002207
        },
        {
          "quantity": 5,
          "msisdn": 2347062021398,
          "otherMSISDN": 2347038834140
        },
        {
          "quantity": 4,
          "msisdn": 2347062021398,
          "otherMSISDN": 2348166692364
        }
      ]
      
      let newData = callDetails.map((item) => {
        return {
          frquency: item.quantity,
          totalRows: item.msisdn,
          frequentNumber: item.otherMSISDN
        }
      });
      console.log(newData)

      【讨论】:

        【解决方案4】:

        使用地图方法。它将遍历所有对象,然后更改它们的每个键。

        var callDetails = [
          {
            quantity: 5,
            msisdn: 1,
            otherMSISDN: 2348032002207
          },
          {
            quantity: 5,
            msisdn: 2347062021398,
            otherMSISDN: 2347038834140
          },
          {
            quantity: 4,
            msisdn: 2347062021398,
            otherMSISDN: 2348166692364
          }
        ];
        
        var res = callDetails.map(item => {
          return {
            frquency: item.quantity,
            totalRows: item.msisdn,
            frequentNumber: item.otherMSISDN
          };
        });
        console.log(res);

        【讨论】:

          【解决方案5】:

          首先,所有答案都是相同的。我只是在补充一些东西。如果您想使用相同的变量并且不想为新变量分配内存,那么您可以这样做:

          var callDetails = [{
              "quantity": 5,
              "msisdn": 1,
              "otherMSISDN": 2348032002207
            },
            {
              "quantity": 5,
              "msisdn": 2347062021398,
              "otherMSISDN": 2347038834140
            },
            {
              "quantity": 4,
              "msisdn": 2347062021398,
              "otherMSISDN": 2348166692364
            }
          ];
          
          for (const detail of callDetails) {
              detail['frquency']= detail.quantity;
              detail['totalRows']= detail.msisdn;
              detail['frequentNumber']= detail.otherMSISDN;
              delete detail.quantity, delete detail.msisdn, delete detail.otherMSISDN;
          }
          
          console.table(callDetails);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-22
            • 1970-01-01
            • 1970-01-01
            • 2019-02-04
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多