I have a below data structure from it i need extract the columns which is an array an i need to build an object with key and actual label name.
Is there any better way to achieve this.
Any help appreciated
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)