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

zqdjd7g9  于 2023-05-27  发布在  Java
关注(0)|答案(6)|浏览(121)

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

noj0wjuj1#

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

d4so4syb2#

所有的答案都是一样的。我只是加了点东西。如果你想使用相同的变量,并且不想为新变量分配内存,那么你可以这样做:

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);
rqcrx0a6

rqcrx0a63#

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);
mrfwxfqh

mrfwxfqh4#

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

hec6srdp5#

使用Map方法。它将循环所有对象,然后更改它们的每个关键点。

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);
ryoqjall

ryoqjall6#

var newdata: dataType[] = data?.map((v, i) => ({
    // ...v,
    newKey1: v.state, // old object value
    newKey2: v.abbreviation,
  }));

相关问题