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
6 Answers
Reset to default 3You 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
评论列表(0条)