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.

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

  1. Filter the entries by whether the key portion of the entry is truthy, then map to the keys:

    const myObject = {
        key1: "foo",
        key2: "",
        key3: "bar",
        key4: "foobar",
        key5: undefined
    };
    
    const keys = Object.entries(myObject)
      .filter(([, val]) => val)
      .map(([key]) => key);
    console.log(keys);
    Reply
  2. If all you care about are the truthy keys you can use a somewhat simpler filter than above:

    myObject = {
        key1: "foo",
        key2: "",
        key3: "bar",
        key4: "foobar",
        key5: undefined
    }
    
    truthyKeys = Object
         .keys(myObject) // array of all keys
         .filter(k => myObject[k]) // filter out ones with falsy values
    
    console.log(truthyKeys)
    Reply
  3. You only need to use Array.filter() to remove the keys that have nullable results

    const myObject = {
        key1: "foo",
        key2: "",
        key3: "bar",
        key4: "foobar",
        key5: undefined
    };
    
    const keys = Object.keys(myObject)
      .filter(key => myObject[key])
    console.log(keys);
    Reply
  4. try it.

    myObject = {
        key1: "foo",
        key2: "",
        key3: "bar",
        key4: "foobar",
        key5: undefined
    }
    
    let x= Object.entries(myObject);
    for(i=0;i<x.length;i++){
    if(x[i][1]!==undefined&&x[i][1]!==""){
    console.log(x[i][0])}
    }
    Reply
  5. const myObject = {
        key1: "foo",
        key2: "",
        key3: "bar",
        key4: "foobar",
        key5: undefined
    };
    
    const keys = Object.keys(myObject).filter(key => myObject[key]);
    

    This will also cut out other falsy values however, such as 0, NaN, null, false. If you very specifically are guarding against empty strings and undefined:

    const myObject = {
        key1: "foo",
        key2: "",
        key3: "bar",
        key4: "foobar",
        key5: undefined
    };
    
    const keys = Object.keys(myObject)
      .filter(key => myObject[key] !== '' && myObject[key] !== undefined);
    
    Reply
  6. let objectWithTruthyKeys = Object.keys(myObject).filter(i => myObject[i]);
    

    Explanation:

    1. Object.keys will get all the keys of the property (including the ones that contain falsy values).
    2. The filter will go through each key in myObject (represented by i). the condition will be true only if the value is truthy, hence, filtering out the keys which contain falsy values.
    Reply
  7. I don’t know if there is another way but you can try something like this

    myObject = {
        key1: "foo",
        key2: "",
        key3: "bar",
        key4: "foobar",
        key5: undefined
    }
    
    const getValue = (obj) => {
        const array = [];
      for (const [key, value] of Object.entries(obj)) {
        if(value){
        array.push(key);
        }
      }
      return array;
    }
    
    
    console.log(getValue(myObject));
    Reply
  8. There’s a couple ways to do that. You could use filter:

    const myObject = {
        key1: "foo",
        key2: "",
        key3: "bar",
        key4: "foobar",
        key5: undefined
    }
            
    var keysWithValues = Object.keys(myObject).filter(key => myObject[key]);
    
    console.log(keysWithValues);

    Or a for loop:

    const myObject = {
      key1: "foo",
      key2: "",
      key3: "bar",
      key4: "foobar",
      key5: undefined
    }
    
    var keysWithValues = [];
    
    for (const [key, value] of Object.entries(myObject)) {
      if (value) {
        keysWithValues.push(key);
      }
    }
    console.log(keysWithValues);

    You could also be fancy and extend Object by creating a class. But I believe that would be overkill 😀

    Reply

Leave a Comment