[英]Find the matching object structure from a complex object
我有一個像下面這樣的示例對象結構
盡管存在三種類型的地址( address, employeeAddress, shippingAddress
),但它們都表示相同的數據結構,稱為 address。 從這個對象結構中,我需要從上述結構中獲取所有地址。對象結構可能是使用 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"
}
}
}
}
}
您可以為此使用遞歸並創建一個接受輸入數據和模式對象的函數。 然后在每個級別上,另一個函數檢查當前對象是否與模式結構匹配。
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)
這是這里找到的一個簡單的 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.