2

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

6 Answers 6

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you all of you
2

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

Comments

1

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

Comments

1

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)

Comments

1

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.