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

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

2

1 Answer 1

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

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

1 Comment

Very useful! i'm learning and I did not know the recursion, I will be investigating it. Thank you!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.