My aim is to calculate the length of the skills array for each user individually.
To begin, I have this JSON data:
const txt = `{
"Alex": {
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e2838e879aa2838e879acc818d8f">[email protected]</a>",
"skills": [
"HTML",
"CSS",
"JavaScript"
],
"age": 20,
"isLoggedIn": false,
"points": 30
},
"Asab": {
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c4d5f4d4e6c4d5f4d4e024f4341">[email protected]</a>",
"skills": [
"HTML",
"CSS",
"JavaScript",
"Redux",
"MongoDB",
"Express",
"React",
"Node"
],
"age": 25,
"isLoggedIn": false,
"points": 50
},
... (remaining content omitted for brevity)
}
`
The next step involves parsing it into the userObj variable:
const userObj = JSON.parse(txt, undefined);
However, the issue arises when trying to iterate over the user objects:
for (let user in userObj) {
console.log(user); //returns string values instead of object references
}
This leads to unexpected outcomes when attempting to process the data. Despite numerous attempts, a suitable solution has yet to be implemented.
Desired output format:
{
Alex => 3,
Asab => 7,
Brooke => 5
}