0

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

3
  • forEach doesn't return anything Commented Oct 3, 2019 at 4:31
  • You're mutating the original object, so just do return params Commented Oct 3, 2019 at 4:35
  • Why not taking advantage of Lodash functions: _.filter(derpObject, (e) => !_.isEmpty(e)). Commented Oct 3, 2019 at 4:47

3 Answers 3

2

.forEach() does not return anything hence undefined output, you might try assigning the values to new object and avoid changing the original object:

function cleanObject(params) {
  const newObject = {};
  Object.keys(params).forEach(key => {
    //console.log(params[key]);
    if (!(_.isNil(params[key]) || _.isEmpty(params[key]))) {
        newObject[key] = params[key]
    }
  });
  //console.log('here', newObject);
  return newObject;
}

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]))) {
        newObject[key] = params[key]
    }
  });
  //console.log('here', newObject);
  return newObject;
}

let result = cleanObject(derpObject);
console.log(result);
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>


Pure lodash:

function cleanObject(params) {
  return _(params).omitBy(_.isNil).omitBy(_.isEmpty);
}

let derpObject = {
  foo: "hey",
  bar: "",
  foo2: [],
  bar2: {}
}

function cleanObject(params) {
  return _(params).omitBy(_.isNil).omitBy(_.isEmpty);
}

console.log(cleanObject(derpObject));
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

0

I think you're almost there. I changed the logic a bit so that it will add the object key and value if the value is valid (not is nil or not is empty)

function cleanObject(params) {
    let newObject = {};
    Object.keys(params).forEach( key => {
        console.log(params[key]);
        if(!_.isNil(params[key]) || !_.isEmpty(params[key])) {
            newObject[key] = params[key]
        }
    });
  console.log('here', newObject);
  return newObject;
}

Comments

0

You can use Array.prototype.reduce() combined with Lodash#isNil() and Lodash#isEmpty()

Code:

const derpObject = {
  foo: "hey",
  bar: "",
  foo2: [],
  bar2: {}
}
const cleanObject = (o) => Object
  .keys(o)
  .reduce((a, k) => {
    if (!(_.isNil(o[k]) || _.isEmpty(o[k]))) {
      a[k] = o[k]
    }
    return a
  },{})
const result = cleanObject(derpObject)

console.log(result)
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.