0

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, false or nullable value.

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

1
  • keys() does what it says, so you'll want a custom filter instead, as someone already wrote in less than a minute. Commented Feb 19, 2021 at 16:11

8 Answers 8

2

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);

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

2 Comments

If they are only interested in the keys - Object.keys has one less iteration
This one definitley worked, but oddly enough the others where Object.keys was used kept returning an empty array for some reason.
2

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);

Comments

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)

Comments

2
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);

Comments

1

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])}
}

Comments

0

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));

Comments

0
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.

Comments

0

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 :D

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.