1

I want to find the attributes I want inside the object.
There is one object.

let obj = {
  1: {
    2 : {
      3: {
        4: {
          5: {

          }
        }
      },
      100: {

      }
    }
  },
  6: {
    7: {
      8: {
      }
    },
    14:{

    }
  },
  11: {
    12: {

    },
    13:{

    }
  }
}
console.log(obj.hasOwnProperty(1)); // true
console.log(obj.hasOwnProperty(6)); // true
console.log(obj.hasOwnProperty(2)); // false
console.log(obj.hasOwnProperty(3)); // false

I want to get true results when I search with 2(others).

What should I do?

1
  • I wrote something similar for another question - it and the other answers might help. Commented Nov 16, 2018 at 7:31

4 Answers 4

4

You'll need a recursive function. One option is to test whether some of the entries either have the property you're looking for, or if the entry's value is an object, and that object passes the recursive test:

let obj={1:{2:{3:{4:{5:{}}},100:{}}},6:{7:{8:{}},14:{}},11:{12:{},13:{}}}

function hasNestedProp(obj, findProp) {
  findProp = String(findProp);
  return Object.entries(obj).some(([key, val]) => {
    return (
      key === findProp
      || typeof val === 'object' && hasNestedProp(val, findProp)
    );
  });
}

console.log(hasNestedProp(obj, 1));
console.log(hasNestedProp(obj, 6));
console.log(hasNestedProp(obj, 2));
console.log(hasNestedProp(obj, 3));
console.log(hasNestedProp(obj, 555));

Or, more concisely, if you pass in string values instead (properties are always strings, after all):

let obj={1:{2:{3:{4:{5:{}}},100:{}}},6:{7:{8:{}},14:{}},11:{12:{},13:{}}}

const hasNestedProp = (obj, findProp) => (
  Object.entries(obj).some(([key, val]) => (
    key === findProp
    || typeof val === 'object' && hasNestedProp(val, findProp)
  ))
)

console.log(hasNestedProp(obj, '1'));
console.log(hasNestedProp(obj, '6'));
console.log(hasNestedProp(obj, '2'));
console.log(hasNestedProp(obj, '3'));
console.log(hasNestedProp(obj, '555'));

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

Comments

1

For nested object, you will need to check for each sub-object as well. Try following

let obj={1:{2:{3:{4:{5:{}}},100:{}}},6:{7:{8:{}},14:{}},11:{12:{},13:{}}}

function hasKey(o, key) {
  for(let k in o) {
    if(k == key) return true;
    else if(typeof o[k] === "object" && hasKey(o[k], key)) return true;
  }
  return false;
}

console.log(hasKey(obj, 1)); // true
console.log(hasKey(obj, 6)); // true
console.log(hasKey(obj, 2)); // true
console.log(hasKey(obj, 3)); // true

Comments

1

you might consider loadash _.get as an option. the following is their example:

var object = { 'a': [{ 'b': { 'c': 3 } }] };

_.get(object, 'a[0].b.c');
// => 3

_.get(object, ['a', '0', 'b', 'c']);
// => 3

_.get(object, 'a.b.c', 'default');
// => 'default'

Comments

1

You can use Object.keys() combined with Array.prototype.reduce()

Code:

const obj = {1:{2:{3:{4:{5:{}}},100:{}}},6:{7:{8:{}},14:{}},11:{12:{},13:{}}};
const hasKey = (obj, key) => Object
  .keys(obj)
  .reduce((a, c) => {
    if (c == key || typeof obj[c] === 'object' && hasKey(obj[c], key)) {
      a = true;
    }
    return a;
  }, false);

console.log(hasKey(obj, 1));
console.log(hasKey(obj, 6));
console.log(hasKey(obj, 2));
console.log(hasKey(obj, 3));
console.log(hasKey(obj, 555));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.