I have the following snippet that is not working and decided to isolate it into a nodejs script:
const _ = require('lodash');
let derpObject = {
foo: "hey",
bar: "",
foo2: [],
bar2: {}
}
function cleanObject(params) {
const newObject = Object.keys(params).forEach( key => {
console.log(params[key]);
if(_.isNil(params[key]) || _.isEmpty(params[key])) {
delete params[key]
}
});
console.log('here', newObject);
return newObject;
}
let result = cleanObject(derpObject);
console.log(result);
Basically my purpose is to check what properties are either empty or null from the main object and remove them and at the end return the new object with just the non empty/null properties.
However, running the above script outputs:
hey
[]
{}
here undefined
undefined
=> undefined
Any idea why I'm getting undefined? I've been banging my head with this one for a while now
forEachdoesn't return anythingreturn params_.filter(derpObject, (e) => !_.isEmpty(e)).