Issue
How to convert this format into an Array. im getting a keyValue pair as below and I have to turn this format to an array in .TS file
countryNew:
{
IN: 159201
BD: 82500
PK: 14237
UA: 486
RU: 9825
}
to This…
countryNew: [
{countryCode: 'IN' , value : 159201},
{countryCode: 'BD' , value : 82500},
{countryCode: 'PK' , value : 14237},
{countryCode: 'UA' , value : 486},
{countryCode: 'RU' , value : 9825},
]
Solution
Object can be easily converted to an array with the help Object.keys()
and Array.prototype.map()
methods
const countryNew = {
IN: 159201,
BD: 82500,
PK: 14237,
UA: 486,
RU: 9825,
};
const result = Object.keys(countryNew)
.map(key => ({ countryCode: key, value: countryNew[key] }));
console.log(result);