我正在處理一個復雜的 NoSQL 資料庫,我的目標是決議其中的資料,分離每個級別的子鍵,然后使用這些鍵來標記網格上的列。通過這種方式,我可以直觀地顯示檔案 Parent -> Child -> Child -> Child 關系。
通過顯示資料和預期輸出,我可能更容易描述資料和任務。
請記住,這是模擬資料,但結構和問題將在這里看到。
[
{
"customer_id": 1,
"customer_name": "John",
"customer_phone": "720-222-1111",
"orders": [
{
"order_id": 1,
"total": 500,
"ordered_from": "website",
"products": [
{
"product_id": 1,
"product_price": 400,
"product_name": "The Blaster",
"product_description": "Blasts everyone away! Fun in the pool"
},
{
"product_id": 2,
"product_price": 100,
"product_name": "Water",
"product_description": "Average H20, delivered to your doorstep",
"product_attributes": [
{
"name": "Addon",
"color": "Blue"
}
]
}
]
},
{
"order_id": 2,
"total": 240,
"ordered_from": "app",
"geolocation": "California",
"coupon_code": "5X23A",
"products": [
{
"product_id": 2
}
]
}
]
},
{
"customer_id": 1,
"customer_name": "Alice",
"customer_address": "23 Main Street",
"customer_zipcode": "15234",
"orders": [
{
"order_id": 4,
"total": 100,
"ordered_from": "website",
"products": [
{
"product_id": 1,
"product_price": 100,
"product_name": "Fins",
"category": "Water"
}
]
},
{
"order_id": 2,
"total": 240,
"ordered_from": "app",
"geolocation": "California",
"coupon_code": "5X23A",
"products": [
{
"product_id": 2
}
]
}
]
},
{
"customer_id": 1,
"customer_name": "Colin",
"customer_gender": "Male",
"orders": [
{
"order_id": 1,
"total": 500,
"ordered_from": "website",
"products": [
{
"product_id": 1,
"product_price": 400,
"product_name": "The Blaster",
"product_description": "Blasts everyone away! Fun in the pool"
},
{
"product_id": 2,
"product_price": 100,
"product_name": "Water",
"product_description": "Average H20, delivered to your doorstep",
"product_attributes": [
{
"name": "Addon",
"color": "Blue"
}
]
}
]
},
{
"order_id": 2,
"total": 240,
"ordered_from": "app",
"geolocation": "California",
"coupon_code": "5X23A",
"products": [
{
"product_id": 2
}
]
}
]
}
]
注意:
- 這里我們有三個NoSql Document,父級物件是客戶
- 這些物件中的資料沒有標準化。一位客戶有一個“customer_phone”鍵,而另一位則沒有。一位客戶有一個“customer_address”鍵,而另一位則沒有。
- 同一點繼續到“訂單”的下一個鍵,它是一個非標準化物件的陣列。一個訂單有一個“地理位置”鍵,而另一個沒有。
- 這種資料的非標準化一直延續到第三個孩子“product_attributes”,有些產品有這個鍵,有些沒有。
我的目標是在每個級別的孩子中獲得每一個可能的鍵。
所以我的預期輸出將是這樣的。
注意:這個 key:value 對中的值根本不重要。我只使用這個物件作為鍵,值可以是任何東西。
[
{
"KEY":"parent",
"customer_id":true,
"customer_phone":true,
"customer_zipcode":true,
"customer_address":true,
"customer_gender":true,
"customer_name":true,
"orders":true,
},
{
"KEY":"parent.orders",
"order_id":true,
"total":true,
"ordered_from":true,
"geolocation":true,
"coupon_code":true,
"products":true,
},
{
"KEY":"parent.orders.products",
"product_id":true,
"product_price":true,
"product_name":true,
"product_description":true,
"product_attributes":true,
"category":true,
},
{
"KEY":"parent.orders.products.attributes",
"name":true,
"color":true
}
]
一旦我有了這個輸出,我就可以輕松地回圈遍歷它并使用鍵在網格組件上創建列。
我的解決方案嘗試:
將第一個共享 JSON 傳遞給此函式
traverseTree(rawData: any, assembled: any[]): any {
let AllKeys: any = {};
let results = [];
rawData.forEach((question: any) => {
for (const [key, value] of Object.entries(question)) {
AllKeys[key] = value;
if (
Array.isArray(value) &&
value.length > 0 &&
typeof value[0] === 'object'
) {
console.log('Count ', value);
assembled = this.traverseTree(mockJSON.questionnaire, []);
}
}
});
results.push(AllKeys);
return [results, ...assembled];
}
如果我洗掉最里面的 if 陳述句,我會得到預期的結果,但僅限于父級,所以我會得到我在上一個 JSON 中共享的第一個物件。
我遇到的另一個問題是資料結構可能會發生變化,因此我無法預測會有多少級別的孩子。
非常感謝任何閱讀本文的人,這對我來說是一個真正的頭腦分裂者。
uj5u.com熱心網友回復:
由于結構是可預測的,因此更容易進行遞回。所以我們正在研究具有屬性的物件陣列。以及每個“陣列”(KEY)的名稱。
所以在陣列的每一層,我們將所有的鍵收集到一個物件中。很簡單。如果我們找到一個陣列,我們對它做同樣的事情(遞回)。唯一的事情是額外的引數是 KEY 的名稱,但這很容易加起來。
var data=[{customer_id:1,customer_name:"John",customer_phone:"720-222-1111",orders:[{order_id:1,total:500,ordered_from:"website",products:[{product_id:1,product_price:400,product_name:"The Blaster",product_description:"Blasts everyone away! Fun in the pool"},{product_id:2,product_price:100,product_name:"Water",product_description:"Average H20, delivered to your doorstep",product_attributes:[{name:"Addon",color:"Blue"}]}]},{order_id:2,total:240,ordered_from:"app",geolocation:"California",coupon_code:"5X23A",products:[{product_id:2}]}]},{customer_id:1,customer_name:"Alice",customer_address:"23 Main Street",customer_zipcode:"15234",orders:[{order_id:4,total:100,ordered_from:"website",products:[{product_id:1,product_price:100,product_name:"Fins",category:"Water"}]},{order_id:2,total:240,ordered_from:"app",geolocation:"California",coupon_code:"5X23A",products:[{product_id:2}]}]},{customer_id:1,customer_name:"Colin",customer_gender:"Male",orders:[{order_id:1,total:500,ordered_from:"website",products:[{product_id:1,product_price:400,product_name:"The Blaster",product_description:"Blasts everyone away! Fun in the pool"},{product_id:2,product_price:100,product_name:"Water",product_description:"Average H20, delivered to your doorstep",product_attributes:[{name:"Addon",color:"Blue"}]}]},{order_id:2,total:240,ordered_from:"app",geolocation:"California",coupon_code:"5X23A",products:[{product_id:2}]}]}]
function parse_obj(data) {
var result = {}
function do_level(arr, name) {
name = name || "PARENT";
var level = {
KEY: name
}
arr.forEach(function(obj) {
Object.keys(obj).forEach(function(key) {
var value = obj[key];
if (Array.isArray(value)) {
do_level(value, name "." key)
}
if (typeof value === 'object' && value !== null) {
// no need. but could have.
}
level[key] = true;
})
})
result[name] = Object.assign({}, result[name], level)
}
do_level(data);
return Object.values(result);
}
console.log(parse_obj(data))
.as-console-wrapper {
max-height: 100% !important
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/505325.html
標籤:javascript json 算法 递归 数据结构
