javascript - how to change array key name in react native - Stack Overflow

I am api response I am getting below array as a response . I have to change the inside key name of arra

I am api response I am getting below array as a response . I have to change the inside key name of array and then send to ui . Please help . I got connfused .

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

// I have to convert my array from above array to below array .

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

I am api response I am getting below array as a response . I have to change the inside key name of array and then send to ui . Please help . I got connfused .

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

// I have to convert my array from above array to below array .

"callDetails": [
        {
          "frquency":5,
           "totalRows":1,
          "frequentNumber": 2348032002207
        },
        {
          "frquency": 5,
          "totalRows": 1,
          "frequentNumber": 2347038834140
        },
        {
          "frquency": 4,
          "totalRows": 1,
          "frequentNumber": 2348166692364
        },
        ]
Share Improve this question asked May 4, 2020 at 10:44 Abhigyan GauravAbhigyan Gaurav 1,9047 gold badges29 silver badges63 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 3

You can use Array.map() to achieve this, something like this may do:

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)

Above all answers are identical. I am just adding up something. If you want to use the same variable and do not want to allocate memory to new variable, then you can do like this:

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

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

You can use map and return an array with object with new key name

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)

Use a map method. It would loop over all the objects and then change each of their keys .

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742312524a4420179.html

相关推荐

  • javascript - how to change array key name in react native - Stack Overflow

    I am api response I am getting below array as a response . I have to change the inside key name of arra

    10小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信