-1
const object = { card: { bank: { id: '123' } } };

I need to know if '123' exists in object. Any way to do that? Thanks!

  • What have you tried ? – j08691 Apr 29 '22 at 20:08
  • also [Finding value in deep nested array of object, recursively, javascript](https://stackoverflow.com/questions/52453666/finding-value-in-deep-nested-array-of-object-recursively-javascript) and [How to find the key of a value in a nested object recursively](https://stackoverflow.com/questions/57676477/how-to-find-the-key-of-a-value-in-a-nested-object-recursively) – pilchard Apr 29 '22 at 20:09

1 Answers1

2

You can use recursion and Array.some to search deeply for the value

const deepExists = (obj, query) =>
  Object.values(obj).some(v => typeof v === "object" ?
    deepExists(v, query) :
    v === query
  );
  
console.log(deepExists({ card: { bank: { id: "123" }}}, "123"));
Asplund
  • 2,254
  • 1
  • 8
  • 19