How to get an array of just the keys that have values attached in Javascript?

So lets say I have an object like this:

myObject = {
    key1: "foo",
    key2: "",
    key3: "bar",
    key4: "foobar",
    key5: undefined
}

and I want an array of the keys, but only if the have a value. i.e. if they’re undefined or empty string, I don’t want them included in the array.

Currently I’m using Object.keys(myObject) but this gets all the keys including those that are undefined/falsy.

I completely understand I can likely write my own version of the keys method from Object, but I’m wondering if theres an easier way than that.

78 thoughts on “How to get an array of just the keys that have values attached in Javascript?”