I am in need of a solution to convert a JavaScript map into an array with a specific format:
[
{
name: "21 years old",
totalUsers: 2,
},
...
]
For example, if I have the following map:
const ages = {
"21": 2,
"18": 10,
}
The function called "parseAges" should return:
[
{
name: "21 years old",
totalUsers: 2,
},
{
name: "18 years old",
totalUsers: 10,
},
]
This is how I am currently accomplishing this using for...of in ES2017:
const ages = {
"21": 2,
"18": 10,
}
function parseAges() {
const agesArr = [];
for (const [key, value] of Object.entries(ages)) {
agesArr.push({
name: `${key} years old`,
totalUsers: value
})
}
return agesArr;
}
console.log(parseAges());
Is there a way to achieve the same result without using for...of, in ES6?