【发布时间】:2019-11-28 08:50:48
【问题描述】:
我有一个如下示例对象结构
尽管有三种类型的地址(address, employeeAddress, shippingAddress),但它们都代表同一个数据结构,称为地址。从这个对象结构中,我需要从上面的结构中获取所有地址。对象结构可以使用 JSON Schema 格式定义。
此外,地址不必总是作为同一层次结构的一部分。例如在上面,shippingAddress 和 employeeAddress 处于不同的层次结构。
我尝试使用对象的 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