Today, I decided to have some fun with the map() function in JavaScript. I am working with an array and trying to return the data from the pages
property. However, I want the page id
to serve as the key, and the index of that page within the pages array to be the value. Can someone help me find out what mistake I'm making?
let result = [
{
"id": 10000089,
"units": [
{
"id": 10000200,
"pages": [
{
"id": 100000882
}
]
},
{
"id": 10000340,
"pages": [
{
"id": 100000912
},
{
"id": 100000915
},
{
"id": 100000919
}
]
}
],
}
];
// Here is my attempt, but it doesn't give the desired output format below
result.flatMap(el => el.units.map((e, i) => (e.pages)));
Desired Output:
pages = [
100000882 => 0,
100000912 => 0,
100000915 => 1,
100000919 => 2,
]
If you'd like to check out the code on Stackblitz, click this link: https://stackblitz.com/edit/js-mc9rqe