我的大脑在跳动。我需要创建一个将接受输入并将其转换为另一种格式的函数。
它应该从对象数组创建一个信息对象,该对象具有按标签排序的数组,并且内部列表值也应具有特殊顺序。我需要找到修复它的方法或算法。
输入内容如下:
[
{
"information": [
{
"label": "Wichtigste Leistungen",
"list": [
{
"list": {
"1953-24uxno": {
"rows": [1]
}
}
},
{
"list": {
"1953-24uxno": {
"rows": [2]
}
}
}
]
},
{
"label": "Allgemein",
"list": [
{
"list": {
"1953-24uxno": {
"rows": [11]
}
}
},
{
"list": {
"1953-24uxno": {
"rows": [12]
}
}
}
]
}
]
},
{
"information": [
{
"label": "Wichtigste Leistungen",
"list": [
{
"list": {
"1953-obbpw8": {
"rows": [3]
}
}
},
{
"list": {
"1953-obbpw8": {
"rows": [4]
}
}
}
]
},
{
"label": "Allgemein",
"list": [
{
"list": {
"1953-obbpw8": {
"rows": [31]
}
}
},
{
"list": {
"1953-obbpw8": {
"rows": [32]
}
}
}
]
}
]
}
]
输出:
{
"information": [
{
"label": "Wichtigste Leistungen",
"list": [
{
"list": {
"1953-24uxno": {
"rows": [1]
},
"1953-obbpw8": {
"rows": [3]
}
}
},
{
"list": {
"1953-24uxno": {
"rows": [2]
},
"1953-obbpw8": {
"rows": [4]
}
}
}
]
},
{
"label": "Allgemein",
"list": [
{
"list": {
"1953-24uxno": {
"rows": [11]
},
"1953-obbpw8": {
"rows": [31]
}
}
},
{
"list": {
"1953-24uxno": {
"rows": [12]
},
"1953-obbpw8": {
"rows": [32]
}
}
}
]
}
]
}
答案 0 :(得分:2)
首先,但不要指望我会为您完成所有工作
const truc1 =
[ { "information":
[ { "label": "Wichtigste Leistungen"
, "list":
[ { "list": { "1953-24uxno": { "rows": [ 1 ] } } }
, { "list": { "1953-24uxno": { "rows": [ 2 ] } } }
]
}
, { "label": "Allgemein"
, "list":
[ { "list": { "1953-24uxno": { "rows": [ 11 ] } } }
, { "list": { "1953-24uxno": { "rows": [ 12 ] } } }
] } ] }
, { "information":
[ { "label": "Wichtigste Leistungen"
, "list":
[ { "list": { "1953-obbpw8": { "rows": [ 3 ] } } }
, { "list": { "1953-obbpw8": { "rows": [ 4 ] } } }
]
}
, { "label": "Allgemein"
, "list":
[ { "list": { "1953-obbpw8": { "rows": [ 31 ] } } }
, { "list": { "1953-obbpw8": { "rows": [ 32 ] } } }
] } ] } ]
const truc2 = truc1.reduce((a,e)=>
{
let k = Object.keys(e)[0]
if (!a[k]) a[k] = []
// continue with e[k]... ( is eq to "information": [...
return a
},{})
console.log( JSON.stringify(truc2,0,2) )
答案 1 :(得分:1)
诀窍是在迭代列表(在i
之后)时跟踪索引label
,以便知道在输出中将列表分配给哪个索引
const data = [{"information":[{"label":"Wichtigste Leistungen","list":[{"list":{"1953-24uxno":{"rows":[1]}}},{"list":{"1953-24uxno":{"rows":[2]}}}]},{"label":"Allgemein","list":[{"list":{"1953-24uxno":{"rows":[11]}}},{"list":{"1953-24uxno":{"rows":[12]}}}]}]},{"information":[{"label":"Wichtigste Leistungen","list":[{"list":{"1953-obbpw8":{"rows":[3]}}},{"list":{"1953-obbpw8":{"rows":[4]}}}]},{"label":"Allgemein","list":[{"list":{"1953-obbpw8":{"rows":[31]}}},{"list":{"1953-obbpw8":{"rows":[32]}}}]}]}]
const out = {}
data.forEach(({ information }) => {
information.forEach(({ label, list }) => {
out[label] = out[label] || { list: [] }
const row = out[label]
// [{list:{19...}}, {list:{19...}}]
list.forEach(({ list }, i) => {
const [k, v] = Object.entries(list)[0]
row.list[i] = row.list[i] || { list: {} }
row.list[i].list[k] = v
})
})
})
console.log(JSON.stringify({ information: out }, null, 2))