Search code examples
javascriptlodash

Find the number of undefined properties in an object with Lodash


let x =    {
      "name": undefined,
      "value": "tr",
      "prop1": undefined,
      "prop2": "test",
      "prop3": 123
    };

I need to find the number of properties that are undefined, so in this case 2.


Solution

  • With regular JS:

    Good comments pointed out that we can indeed immediately filter; more efficient:

    Object.values(x).filter(v=> v === undefined).length
    

    With Lodash

    _.filter(x, (v, k) => v === undefined).length