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

时间:2019-07-19 11:01:44

标签: javascript lodash

我有一个类似于下面的示例对象结构

即使存在三种类型的地址(address, employeeAddress, shippingAddress),它们都表示称为地址的相同数据结构。从这个对象结构中,我需要从上面的结构中获取所有地址。对象结构可能是使用JSON模式格式定义的。 同样,地址不必始终是同一层次结构的一部分。例如,在上文中,shippingAddressemployeeAddress位于不同的层次结构。 我尝试使用对象的hasOwnProperty,但没有按预期方式工作。也没有从filter中的lodash方法获得太多帮助。有没有达到此目的的优雅方法?

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

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

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