我有以下陣列,它是展平的,所以我想將它轉換為嵌套陣列。
const data = [{
"countryId": 9,
"countryName": "Angola",
"customerId": 516,
"customerName": "CHEVRON (SASBU) - ANGOLA",
"fieldId": 2000006854,
"fieldName": "A12"
},
{
"countryId": 9,
"countryName": "Angola",
"customerId": 516,
"customerName": "CHEVRON (SASBU) - ANGOLA",
"fieldId": 5037,
"fieldName": "BANZALA"
}
]
我想要如下界面
interface Option {
value: number,
viewText: string,
ids?: Set<number>,
children?: Option[]
}
所以輸出應該是這樣的
const output = [{
"value": 9,
"viewText": "Angola",
"ids": [516],
"children": [{
"value": 516,
"viewText": "CHEVRON (SASBU) - ANGOLA",
"ids": [2000006854],
"children": [{
"viewText": "A12",
"value": 2000006854
}]
}]
}]
我一直在努力解決這個問題,我不知道如何解決這個問題,我是否需要使用遞回函式或其他東西,有什么想法嗎?
更新:
這是我天真的解決方案。這種方法的問題是我手動處理嵌套結構。
function mapFlatToNested(data: typeof dataset) {
const map = new Map<number, Option>()
for (const item of data) {
const itemFromMap = map.get(item.countryId);
if (!itemFromMap) {
map.set(item.countryId, {
viewText: item.countryName,
value: item.countryId,
ids: new Set([item.customerId]),
childrens: [{
viewText: item.customerName,
value: item.customerId,
ids: new Set([item.fieldId]),
childrens: [{
value: item.fieldId,
viewText: item.fieldName
}]
}]
})
} else {
if (!itemFromMap?.ids?.has(item.customerId)) {
itemFromMap?.ids?.add(item.customerId);
itemFromMap?.childrens?.push({
value: item.customerId,
viewText: item.customerName,
ids: new Set([item.fieldId]),
childrens: [{
value: item.fieldId,
viewText: item.fieldName
}]
})
} else {
const customer = itemFromMap?.childrens?.find((customer) => customer.value === item.customerId);
if (customer) {
if (!customer.ids?.has(item.fieldId)) {
customer.ids?.add(item.fieldId)
customer.childrens?.push({
value: item.fieldId,
viewText: item.fieldName,
ids: new Set([customer.value])
})
}
}
}
}
}
return map
}
console.log(mapFlatToNested(dataset));
這是打字稿游樂場
uj5u.com熱心網友回復:
此版本允許您像這樣配置您的功能:
const transform = nestedGroup ('country', 'customer', 'field')
transform (dataset)
'value'它不是特別通用,嵌入'viewText'到代碼中,并假設原始欄位看起來像<something>Idand <something>Name。但它完成了這項作業,并且受到這些限制,是可配置的。
const nestedGroup = (level, ...levels) => (xs, id = level 'Id', name = level 'Name') =>
level == undefined
? xs
: Object .values (xs .reduce (
(a, x, _, __, k = x [id] '~' x [name]) => ((a [k] = [... (a [k] || []), x]), a),
{}
)) .map (xs => ({
value: xs [0] [id],
viewText: xs [0] [name],
... (levels .length > 0 ? {
ids: xs .map (x => x [`${levels [0]}Id`]),
children: nestedGroup (...levels)(xs)
} : {}),
}))
const transform = nestedGroup ('country', 'customer', 'field')
const dataset = [{countryId: 59, countryName: "Algeria", customerId: 6959, customerName: "ABERDEEN DRILLING SCHOOL LTD", fieldId: 8627, fieldName: " BAGAN"}, {countryId: 59, countryName: "Algeria", customerId: 2730, customerName: "ABU DHABI COMPANY FOR ONSHORE OIL OPERATIONS", fieldId: 6158, fieldName: "BAB"}, {countryId: 59, countryName: "Algeria", customerId: 3457, customerName: "AGIP - ALGIERS", fieldId: 9562, fieldName: "LESS"}, {countryId: 9, countryName: "Angola", customerId: 516, customerName: "CHEVRON (SASBU) - ANGOLA", fieldId: 2000006854, fieldName: "A12"}, {countryId: 9, countryName: "Angola", customerId: 516, customerName: "CHEVRON (SASBU) - ANGOLA", fieldId: 5037, fieldName: "BANZALA"}]
console .log (transform (dataset))
.as-console-wrapper {max-height: 100% !important; top: 0}
我們從創造id和name價值開始。在第一級,它們將是countryIdand countryName,然后是customerIdand customerName,依此類推。
然后,如果我們在底層(沒有更多的層次可以嵌套),我們只需回傳我們的輸入。[id]否則,我們將具有相同and值的這些值組合在一起[name],然后對于每個組,我們提取公共欄位,以及(如果有更多嵌套要做ids,遞回地,children.
讓這個更通用會很有趣。我認為嘗試提出一個采用這種配置的函式會很有趣:
const transform = nestedGroup ('children', [
{value: 'countryId', viewText: 'countryName', ['ids?']: (xs) => xs .map (x => x.id)},
{value: 'customerId', viewText: 'customerName', ['ids?']: (xs) => xs .map (x => x.id)},
{value: 'fieldId', viewText: 'fieldName', ['ids?']: (xs) => xs .map (x => x.id)},
])
where the '?' on ids marks it as optional: used only if there are further nested levels. we could then write your transformation as an easy gloss that accepts ['country', 'customer', 'field'] and generates the above. This would let us group on as many fields as we like and then rename them as we chose. The first argument, , 'children', is just to allow us to name descendent nodes. It would probably be better to include that per-level as well, but I don't have that quite figured out.
I think that would be an interesting and useful function, but I haven't worked out the details yet. Perhaps I'll come back to it soon.
uj5u.com熱心網友回復:
您需要映射您的陣列:
data.map(cur=>({
value:cur.countryId,
viewText:cur.countryName,
ids:[cur.customerId],
children:[{
value:cur.customerId,
viewText:cur.customerName,
ids:[cur.fieldId],
children:[{
viewText:cur.fieldName,
value:cur.fieldId
}]
}]
}))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/422778.html
標籤:
上一篇:python中的普羅尼克數
