I need to take this object, check each properties truthyness, and then remove the untruthy ones.
var user = {
name: 'my name',
email: null,
pwHash: 'U+Ldlngx2BYQk',
birthday: undefined,
username: 'myname33',
age: 0
}
Here is the code I was trying
function truth(x) {
if (x) {
console.log("truthy");
} else {
delete;
}
}
for (x in user) {
truth(user[x]);
}
but it's not working and I'm not even sure I fully understand how to make sure I'm checking truthy right. what am I doing wrong?
You have to specify both the object and the property when you delete it.
It's not enough with just the value copied from the property that you pass to the function, so you can't do it in that function (unless you also pass in the object reference and the property name).
for (x in user) {
if (user[x]) {
console.log("truthy");
} else {
delete user[x];
}
}