Reputation: 93
completely new to nodeJS so please bear with me.
I need some help regarding a functionality I am trying to create, basically I am trying to return all the empty, null or undefined values from an object using a filter but not sure how to go about it.
This is some code I found online which shows how to delete empty values but rather than delete, I want it to display all the values that are null, undefined or empty (this would include the key and the value).
The code is as follows:
const myObj = {
a: 1,
b:'foo',
c: '',
d: null,
e: undefined,
f: {v: 1, w:'foo', x: '', y: null, z: undefined, m:{a:'asd'}}
};
const removeEmpty = (obj) => {
Object.keys(obj).forEach(k =>
(obj[k] && typeof obj[k] === 'object') && removeEmpty(obj[k]) ||
(!obj[k] && obj[k] !== undefined)
);
return obj;
};
console.log(removeEmpty(myObj));
All help is much appreciated.
Thanks for reading
Upvotes: 0
Views: 73
Reputation: 314
I'd write a function which recursively go through all values of the object and delete the ones not needed. In our case we don't need valid values.
I've following code in mind
function deleteValues(myObj) {
for (let key in myObj) {
if (myObj[key]) {
if (typeof myObj[key] == 'object') {
deleteValues(myObj[key]);
} else {
delete myObj[key];
}
}
}
}
deleteValues(myObj);
Upvotes: 0
Reputation: 1
for (const key in myObj) if ([null, undefined].includes(myObj[key])) console.log(`myObj[${key}] is ${myObj[key]}`);
This would show you your undefined or null values in that object, which is all you needed right? Unless I read the question wrong.
Upvotes: 0
Reputation: 2658
Not sure where you got your snippet of code, but it doesn't seem to be modifying the object in any way. The below should work, commented to hopefully help your understanding of it. Since objects are passed by reference, we can recurse on any property which is an object to handle the nested cases.
const myObj = {
a: 1,
b:'foo',
c: '',
d: null,
e: undefined,
f: {v: 1, w:'foo', x: '', y: null, z: undefined, m:{a:'asd'}}
};
const removeEmpty = (obj) => {
Object.keys(obj).forEach(k => {//for each property of the object
if(obj[k] === null){
delete obj[k];//special case for null since typeof null returns "object" :-(
}else if(typeof obj[k] === 'object'){//if the property IS an object
removeEmpty(obj[k]);//recurse
}else if(! obj[k]){//if property is falsey (undefined, "", 0)
delete obj[k];//remove it from the object
}
});
return obj;
};
console.log(removeEmpty(myObj));
EDIT
if you're looking to get a object which only contains empty values, you could modify the code as such:
const myObj = {
a: 1,
b:'foo',
c: '',
d: null,
e: undefined,
f: {v: 1, w:'foo', x: '', y: null, z: undefined, m:{a:'asd'}}
};
const removeNotEmpty = (obj) => {
Object.keys(obj).forEach(k => {//for each property of the object
if(typeof obj[k] === 'object' && obj[k] !== null){//if the property IS an object
removeNotEmpty(obj[k]);//recurse
}else if(obj[k]){//if property is truthy
delete obj[k];//remove it from the object
}
});
return obj;
};
console.log(removeNotEmpty(Object.assign({},myObj)));//make sure to copy the object if you don't want to modify the first (that's what the Object.assign is for)
Upvotes: 1