как изменить имя ключа массива в реагировать родной - PullRequest
0 голосов
/ 04 мая 2020

Я получаю ответ API, получаю ответ ниже массива. Я должен изменить внутреннее имя ключа массива и затем отправить пользовательскому интерфейсу. Пожалуйста помоги . Я запутался.

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

Ответы [ 5 ]

2 голосов
/ 04 мая 2020

Вы можете использовать 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)
1 голос
/ 04 мая 2020

Вы можете использовать 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)
1 голос
/ 04 мая 2020

Используйте метод карты. Это будет l oop на все объекты, а затем изменить каждый из их ключей.

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);
1 голос
/ 04 мая 2020

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);
0 голосов
/ 05 мая 2020

Выше всех ответов одинаковы. Я просто что-то складываю. Если вы хотите использовать одну и ту же переменную и не хотите выделять память для новой переменной, то вы можете сделать это так:

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