gpt4 book ai didi

javascript - 过滤键并减少为嵌套结构

转载 作者:行者123 更新时间:2023-11-30 14:00:30 24 4
gpt4 key购买 nike

我有一个下面的数据结构,我需要提取列,这是一个数组,我需要构建一个具有键和实际标签名称的对象。

有没有更好的方法来实现这一点。

感谢任何帮助

    let data = {
"section1": {
"forms": [
{
"fields": [
{
"columnName": "test1",
"label": [
{
"actualLabel": "Test 1"
}
]
},
{
"columnName": "test2",
"label": [
{
"actualLabel": "Test 2"
}
]
},
{
"columnName": "test0",
"label": [
{
"actualLabel": "Test 0"
}
]
}
]
},
{
"fields": [
{
"columnName": "test6",
"label": [
{
"actualLabel": "Test 6"
}
]
},
{
"columnName": "test3",
"label": [
{
"actualLabel": "Test 3"
}
]
},
{
"columnName": "test10",
"label": [
{
"actualLabel": "Test 10"
}
]
}
]
},
{
"fields": [
{
"columnName": "test15",
"label": [
{
"actualLabel": "Test 15"
}
]
},
{
"columnName": "test",
"label": [
{
"actualLabel": "Test 6"
}
]
},
{
"columnName": "test7",
"label": [
{
"actualLabel": "Test 7"
}
]
}
]
}
]
},
"section2": {
"forms": [
{
"fields": [
{
"columnName": "test1",
"label": [
{
"actualLabel": "Test 1"
}
]
},
{
"columnName": "test2",
"label": [
{
"actualLabel": "Test 2"
}
]
},
{
"columnName": "test0",
"label": [
{
"actualLabel": "Test 0"
}
]
}
]
},
{
"fields": [
{
"columnName": "test3",
"label": [
{
"actualLabel": "Test 3"
}
]
},
{
"columnName": "test4",
"label": [
{
"actualLabel": "Test 4"
}
]
},
{
"columnName": "test10",
"label": [
{
"actualLabel": "Test 10"
}
]
}
]
},
{
"fields": [
{
"columnName": "test5",
"label": [
{
"actualLabel": "Test 5"
}
]
},
{
"columnName": "test6",
"label": [
{
"actualLabel": "Test 6"
}
]
},
{
"columnName": "test7",
"label": [
{
"actualLabel": "Test 7"
}
]
}
]
}
]
},
"section3": {
"forms": [
{
"fields": [
{
"columnName": "test1",
"label": [
{
"actualLabel": "Test 1"
}
]
},
{
"columnName": "test2",
"label": [
{
"actualLabel": "Test 2"
}
]
},
{
"columnName": "test0",
"label": [
{
"actualLabel": "Test 0"
}
]
}
]
},
{
"fields": [
{
"columnName": "test3",
"label": [
{
"actualLabel": "Test 3"
}
]
},
{
"columnName": "test 4",
"label": [
{
"actualLabel": "Test 4"
}
]
},
{
"columnName": "test10",
"label": [
{
"actualLabel": "Test 10"
}
]
}
]
},
{
"fields": [
{
"columnName": "test15",
"label": [
{
"actualLabel": "Test 15"
}
]
},
{
"columnName": "test6",
"label": [
{
"actualLabel": "Test 6"
}
]
},
{
"columnName": "test7",
"label": [
{
"actualLabel": "Test 7"
}
]
}
]
}
]
}
}


let extractColumns = ['test1', 'test2', 'test7', 'test15']

let result = Object.entries(data).reduce(
(initial, [key, { forms }]) => {
forms.forEach(({ fields }) => {
fields.forEach(
({
columnName,
label: {
0: { actualLabel },
},
}) => {
if (extractColumns.indexOf(columnName) > -1) {
initial[columnName] = {
actualLabel,
};
}
},
);
});
return initial;
},
{},
);

console.log(result)

最佳答案

您可以像这样简化和压缩代码 - 使用 Object.valuesincludes、三元运算符和简化的数组解构。

let data={"section1":{"forms":[{"fields":[{"columnName":"test1","label":[{"actualLabel":"Test 1"}]},{"columnName":"test2","label":[{"actualLabel":"Test 2"}]},{"columnName":"test0","label":[{"actualLabel":"Test 0"}]}]},{"fields":[{"columnName":"test6","label":[{"actualLabel":"Test 6"}]},{"columnName":"test3","label":[{"actualLabel":"Test 3"}]},{"columnName":"test10","label":[{"actualLabel":"Test 10"}]}]},{"fields":[{"columnName":"test15","label":[{"actualLabel":"Test 15"}]},{"columnName":"test","label":[{"actualLabel":"Test 6"}]},{"columnName":"test7","label":[{"actualLabel":"Test 7"}]}]}]},"section2":{"forms":[{"fields":[{"columnName":"test1","label":[{"actualLabel":"Test 1"}]},{"columnName":"test2","label":[{"actualLabel":"Test 2"}]},{"columnName":"test0","label":[{"actualLabel":"Test 0"}]}]},{"fields":[{"columnName":"test3","label":[{"actualLabel":"Test 3"}]},{"columnName":"test4","label":[{"actualLabel":"Test 4"}]},{"columnName":"test10","label":[{"actualLabel":"Test 10"}]}]},{"fields":[{"columnName":"test5","label":[{"actualLabel":"Test 5"}]},{"columnName":"test6","label":[{"actualLabel":"Test 6"}]},{"columnName":"test7","label":[{"actualLabel":"Test 7"}]}]}]},"section3":{"forms":[{"fields":[{"columnName":"test1","label":[{"actualLabel":"Test 1"}]},{"columnName":"test2","label":[{"actualLabel":"Test 2"}]},{"columnName":"test0","label":[{"actualLabel":"Test 0"}]}]},{"fields":[{"columnName":"test3","label":[{"actualLabel":"Test 3"}]},{"columnName":"test 4","label":[{"actualLabel":"Test 4"}]},{"columnName":"test10","label":[{"actualLabel":"Test 10"}]}]},{"fields":[{"columnName":"test15","label":[{"actualLabel":"Test 15"}]},{"columnName":"test6","label":[{"actualLabel":"Test 6"}]},{"columnName":"test7","label":[{"actualLabel":"Test 7"}]}]}]}};
let extractColumns = ['test1', 'test2', 'test7', 'test15'];

const result = Object.values(data).reduce((acc, { forms }) => {
forms.forEach(({ fields }) => fields.forEach(({ columnName, label: [{ actualLabel }]}) => extractColumns.includes(columnName) ? acc[columnName] = { actualLabel } : null));
return acc;
}, {});

console.log(result)

关于javascript - 过滤键并减少为嵌套结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56392294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com