Ok so I know the title is a lil confusing (maybe a lot) so let me explain. In an Object variable like a mongoose schema there can be types as String, Number, Boolean, etc... But what if the type is a new Object? And what if that Object has another Object inside?
Let's say I got this schema in here:
user: {
username: 'bob1',
password: '1234',
email: 'someone@example.com',
profile: {
name: {
first: 'Bob',
last: 'Marley'
},
gender: 'male',
country: 'US'
},
userKeys: {
username: { show: true },
password: { show: false },
email: { show: 'maybe' },
profile: {
name: {
first: { show: true },
last: { show: true }
},
gender: { show: false },
country: { show: 'maybe' }
}
}
}
and I want to create a new temporary Object in my file based on that user's userKeys
returning only the values from the schema that have the value true
or 'maybe'
from the userKeys
fields. How can I do that by iterating not only the keys from the basic structure but the keys inside the Object that is inside another Object? I have tried to do it with
Object.keys(user.userKeys).forEach((key) => {
const cur = user[key];
if (typeof cur === Object) {
Object.keys(user.userKeys[key]).forEach((ok) => {
console.log(ok, user.userKeys[ok].show);
myObj.push(cur);
});
}
});
but it won't log anything that is a part of an Object (example: profile.name or profile.country).
I appreciate any tips.
Have a nice day