0

I'm trying to use an if (key in object) statement to get if a key exists in an object, however I've noticed that it only works for the first level of an object, like this:

myObject = {
 name: 'Bob',
 age: 20,
 dog: true
}

However, it doesn't seem to work with nested objects like this:

myObject = {
 name: 'Joe',
 favorites: {
 ice_cream: 'vanilla',
 coffee: 'espresso'
 }
}

If I were to run console.log('coffee' in myObject), it would return false. I've also tried using myObject.hasOwnProperty('coffee'), however it still returns false.

asked Jun 1, 2021 at 5:31
1

2 Answers 2

1

If you really want to be able to check if the key exists anywhere inside the object recursively, you'll need a recursive function.

const myObject = {
 name: 'Joe',
 favorites: {
 ice_cream: 'vanilla',
 coffee: 'espresso'
 }
};
const getKeys = (obj, keys = [], visited = new Set()) => {
 if (visited.has(obj)) return;
 visited.add(obj);
 keys.push(...Object.keys(obj));
 for (const val of Object.values(obj)) {
 if (val && typeof val === 'object') {
 getKeys(val, keys, visited);
 }
 }
 return keys;
};
console.log(getKeys(myObject));
console.log(getKeys(myObject).includes('coffee'));

But while this is possible, it's really weird. I would hope never to see anything like this in serious code.

answered Jun 1, 2021 at 5:34
2
  • What do you think the best approach is for checking if a key exists? I'm working with a pretty big object which is an item's decoded NBT from Minecraft, and different items have different fields, so I can't just write the same thing that works for every item. Commented Jun 1, 2021 at 5:42
  • Figure out what the object's structure can be like in advance so you can go right to the possible property you want to find. This may involve array or object methods - that's fine - but iterating over the whole object all the way down is a mark of not knowing enough about your input. Commented Jun 1, 2021 at 5:44
0

You can try this,

function getKey(obj) {
 let objectKeys = [];
 let keyValues = Object.entries(obj);
 for (let i in keyValues) {
 objectKeys.push(keyValues[i][0]);
 if (typeof keyValues[i][1] == "object") {
 var keys = getKey(keyValues[i][1])
 objectKeys = objectKeys.concat(keys);
 }
 }
 return objectKeys;
}

You can pass the key to the function and check,

 let key = getKey(coffee);
 console.log(key);
answered Jun 1, 2021 at 5:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.