0

Before any one sais Object.keys, that may not work here. Please read on before voting to close or commenting.

Consider the following object and the value being passed in:

enter image description here

As you can see I have a key, which doesn't exist in this object. But the intention is that the key being passed in might exist some where in this object and if it does I want to return the value of the hide.

So an example would be something like:

// Pseudo code, `object` is the object in the screen shot.
if (object.hasKey('date_of_visit')) {
  return object.find('date_of_visit').hide
}

Everything I have ever found on stack and the webs is "find the key by the value." I do not have the value, I just have a potential key. I have looked at lodash and underscore and a bunch of stack questions but have found nothing.

Any ideas or help would be greatly appreciated. The object nesting should not matter. If I passed in other_cause_of_death I should get back true.

Thoughts?

Edit:

const object = {
  status: {
    cause_of_death: {
      hide: true,
      other_cause_of_death: {
        hide: true
      }
    }
  }
};

Heres a simplified version of the object. Same rules should still apply.

TheWebs
  • 12,470
  • 30
  • 107
  • 211
  • Check this out: https://gist.github.com/creationix/7473758#file-extract-closure-js – Shubham Agrawal Jun 04 '19 at 21:57
  • Recursion? can solve it for one — solve for all?? – philipp Jun 04 '19 at 21:57
  • @ShubhamAgrawal Your possible answer only returns keys. – TheWebs Jun 04 '19 at 21:58
  • @philipp if I knew how to do it, in a clean, concise way - trust. I would. Hence the asking for help. – TheWebs Jun 04 '19 at 21:59
  • Can you please paste the object as text and not an image? – slider Jun 04 '19 at 21:59
  • @slider Posted a simplified version of the object. – TheWebs Jun 04 '19 at 22:05
  • @TheWebs: My understanding: you only want to know whether or not the key exists in the object. One easy solution is that in the returned array from above function, check for the desired key using array.includes. Another option is to modify the function above to stop once the key is found and return true. – Shubham Agrawal Jun 04 '19 at 22:07
  • @ShubhamAgrawal These are not actual functions. This is "fake code" to give you an idea of how I would like to implement this, but don't know how on such nested objects in a clean concise way. There are two parts to this: Check if the key exists any where in the object and get the hidden value from said object, if it exists. – TheWebs Jun 04 '19 at 22:09
  • Possible duplicate of [Test for existence of nested JavaScript object key](https://stackoverflow.com/questions/2631001/test-for-existence-of-nested-javascript-object-key) – Heretic Monkey Jun 04 '19 at 22:12

2 Answers2

1

You can use a recursive approach (DFS) to find the object next to your key. If a non-null object is returned, you can get its hide value:

const data = {
  status: {
    cause_of_death: {
      hide: true,
      other_cause_of_death: {
        hide: true
      }
    },
    date_of_birth: {
      hide: true
    }
  }
};

function findKey(obj, key) {
  if (typeof obj !== 'object') return null;
  if (key in obj) return obj[key];
  for (var k in obj) {
    var found = findKey(obj[k], key);
    if (found) return found;
  }
  return null;
}

console.log(findKey(data, 'date_of_birth'));
console.log(findKey(data, 'cause_of_death'));
console.log(findKey(data, 'other_cause_of_death'));
console.log(findKey(data, 'hello'));
slider
  • 12,810
  • 1
  • 26
  • 42
  • 1
    damn that was fast. I see what you did here, I would have done that in like 9 nested for loops. LOL. Thanks so much man! – TheWebs Jun 04 '19 at 22:10
0

Since you're working with some structured data this could be a valid approach:

It follows the Immutable.js approach to how getting stuff from immutable maps works.

This will return undefined for an invalid key path.

function getIn(obj, keyPath) {
  return keyPath.reduce((prev, curr) => {
    return Object.keys(prev).length ? prev[curr] : obj[curr];
  }, {});
}

const res = getIn(
    data, ['status', 'cause_of_death', 'other_cause_of_death', 'hide']
);
Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
  • As you can see I only pass in one value at a time and @slider's response works much better for my use case. – TheWebs Jun 04 '19 at 22:14