My goal is to map the length of the skills array for each user.
I start with this bit of JSON here:
const txt = `{
"Alex": {
"email": "[email protected]",
"skills": [
"HTML",
"CSS",
"JavaScript"
],
"age": 20,
"isLoggedIn": false,
"points": 30
},
"Asab": {
"email": "[email protected]",
"skills": [
"HTML",
"CSS",
"JavaScript",
"Redux",
"MongoDB",
"Express",
"React",
"Node"
],
"age": 25,
"isLoggedIn": false,
"points": 50
},
"Brook": {
"email": "[email protected]",
"skills": [
"HTML",
"CSS",
"JavaScript",
"React",
"Redux"
],
"age": 30,
"isLoggedIn": true,
"points": 50
},
"Daniel": {
"email": "[email protected]",
"skills": [
"HTML",
"CSS",
"JavaScript",
"Python"
],
"age": 20,
"isLoggedIn": false,
"points": 40
},
"John": {
"email": "[email protected]",
"skills": [
"HTML",
"CSS",
"JavaScript",
"React",
"Redux",
"Node.js"
],
"age": 20,
"isLoggedIn": true,
"points": 50
},
"Thomas": {
"email": "[email protected]",
"skills": [
"HTML",
"CSS",
"JavaScript",
"React"
],
"age": 20,
"isLoggedIn": false,
"points": 40
},
"Paul": {
"email": "[email protected]",
"skills": [
"HTML",
"CSS",
"JavaScript",
"MongoDB",
"Express",
"React",
"Node"
],
"age": 20,
"isLoggedIn": false,
"points": 40
}
}
`
I then parse it to the userObj variable:
const userObj = JSON.parse(txt, undefined);
Now this is where things get odd,
for (let user in userObj) {
console.log(user); //returns several strings, rather than the objects themselves
}
and when I try to iterate over those objects, naturally I get unexpected results. I've tried many different approaches over the past few hours and haven't managed to implement anything that works.
Expected final result example:
{
Alex => 3,
Asab => 7,
Brooke => 5
}
userObj[user]to get the objects