【问题标题】:Find the matching object structure from a complex object从复杂对象中找到匹配的对象结构
【发布时间】:2019-11-28 08:50:48
【问题描述】:

我有一个如下示例对象结构

尽管有三种类型的地址(address, employeeAddress, shippingAddress),但它们都代表同一个数据结构,称为地址。从这个对象结构中,我需要从上面的结构中获取所有地址。对象结构可以使用 JSON Schema 格式定义。 此外,地址不必总是作为同一层次结构的一部分。例如在上面,shippingAddressemployeeAddress 处于不同的层次结构。 我尝试使用对象的 hasOwnProperty,但没有按预期工作。 lodash 中的 filter 方法也没有得到太多帮助。有没有一种优雅的方法来实现这一点?

{
  "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"
        }
      }
    }
  }
}

【问题讨论】:

  • 没错@NenadVracar。我想得到匹配的结构

标签: javascript lodash


【解决方案1】:

您可以为此使用递归并创建一个接收输入数据和模式对象的函数。然后在每个级别上,另一个函数检查当前对象是否匹配模式结构。

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)

【讨论】:

  • const 'types' 实际上是未知的。我只知道“地址”的结构
  • @Apps 你会如何定义结构?
  • 类似于 JSON 模式的东西。如果它是对象,我们在其中拥有类型和属性。
  • 查看更新,您可以使用模式和匹配功能,但原理保持不变。
【解决方案2】:

这是一个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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多