简体   繁体   English

从复杂对象中找到匹配的对象结构

[英]Find the matching object structure from a complex object

I have a sample object structure like below我有一个像下面这样的示例对象结构

Even though there are three types of addresses ( address, employeeAddress, shippingAddress ), they all represent the same data structure called address.尽管存在三种类型的地址( address, employeeAddress, shippingAddress ),但它们都表示相同的数据结构,称为 address。 From this object structure, I need to get all the addresses from the above structure.The object structure might be defined in using a JSON Schema format.从这个对象结构中,我需要从上述结构中获取所有地址。对象结构可能是使用 JSON Schema 格式定义的。 Also the addresses need not be always as part of the same hierarchy.此外,地址不必总是作为同一层次结构的一部分。 For example in the above, shippingAddress and employeeAddress are at different hierarchy.例如在上面, shippingAddressemployeeAddress处于不同的层次结构。 I tried with object's hasOwnProperty, but did not work the way as expected.我尝试使用对象的 hasOwnProperty,但没有按预期工作。 Did not get much help from the filter method in lodash also. lodashfilter方法也没有得到太多帮助。 Is there an elegant way to achieve this?有没有一种优雅的方法来实现这一目标?

{
  "user": {
    "firstName": "John",
    "lastName": "Steve",
    "address": {
      "houseNo": "24",
      "city": "CA",
      "country": {
        "code": "US",
        "name": "United States"
      }
    }
  },
  "employee": {
    "employeeID": "443434",
    "employeeName": "Steve",
    "employeeAddress": {
      "houseNo": "244",
      "city": "NJ",
      "country": {
        "code": "US",
        "name": "United States"
      }
    }
  },
  "assistant": {
    "assitantID": "443434",
    "employeeName": "Steve",
    "shippingDetails": {
      "shippingAddress": {
        "houseNo": "2444",
        "city": "LA",
        "country": {
          "code": "US",
          "name": "United States"
        }
      }
    }
  }
}

You could use recursion for this and create a function that takes input data and schema object.您可以为此使用递归并创建一个接受输入数据和模式对象的函数。 Then on each level another function checks if the current object matches schema structure.然后在每个级别上,另一个函数检查当前对象是否与模式结构匹配。

 const data = {"user":{"firstName":"John","lastName":"Steve","address":{"houseNo":"24","city":"CA","country":{"code":"US","name":"United States"}}},"employee":{"employeeID":"443434","employeeName":"Steve","employeeAddress":{"houseNo":"244","city":"NJ","country":{"code":"US","name":"United States"}}},"assistant":{"assitantID":"443434","employeeName":"Steve","shippingDetails":{"shippingAddress":{"houseNo":"2444","city":"LA","country":{"code":"US","name":"United States"}}}}} const schema = { houseNo: null, country: null, city: null } function match(o1, o2) { return Object.keys(o1).every(k => k in o2); } function get(data, schema) { return Object.keys(data).reduce((r, e) => { if (match(data[e], schema)) r.push(data[e]); else if (typeof data[e] == 'object') r.push(...get(data[e], schema)); return r; }, []) } const result = get(data, schema); console.log(result)

Here is a plain JS version of one found here这是这里找到的一个简单的 JS 版本

 var user = { "user": { "firstName": "John", "lastName": "Steve", "address": { "houseNo": "24", "city": "CA", "country": { "code": "US", "name": "United States" } } }, "employee": { "employeeID": "443434", "employeeName": "Steve", "employeeAddress": { "houseNo": "244", "city": "NJ", "country": { "code": "US", "name": "United States" } } }, "assistant": { "assitantID": "443434", "employeeName": "Steve", "shippingDetails": { "shippingAddress": { "houseNo": "2444", "city": "LA", "country": { "code": "US", "name": "United States" } } } } } function findProp(obj, prop) { var result = {}; function recursivelyFindProp(o, keyToBeFound) { Object.keys(o).forEach(function (key) { if (typeof o[key] === 'object') { if (key.toLowerCase().indexOf(keyToBeFound) !==-1) result[key]=o[key]; recursivelyFindProp(o[key], keyToBeFound); } else { if (key.toLowerCase().indexOf(keyToBeFound) !==-1) result[key]=o[key]; } }); } recursivelyFindProp(obj, prop); return result; } console.log( findProp(user, "address") )
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

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

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