简体   繁体   English

JavaScript查找对象属性

[英]JavaScript find Object Property

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). 当我使用2(others)搜索时,我想获得真实的结果。

What should I do? 我该怎么办?

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: 一种选择是测试some entries是否具有您要查找的属性,或者条目的值是否是一个对象,并且该对象通过了递归测试:

 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): 或者,更简洁地说,如果您传入的是字符串值(毕竟,属性始终是string ):

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

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 

you might consider loadash _.get as an option. 您可以考虑将loadash _.get作为选项。 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'

You can use Object.keys() combined with Array.prototype.reduce() 您可以将Object.keys()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)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM