I have an array of objects called response where each key has a value, now I need to return an array with this key and value but need to fetch the label from another object which contains the label name for the key.
The label name for the key is nested level, inside data variable.
How can I get the expected data as shown below. need to select the sectionType from response and find the label.
I need to get the expectedData variable result by merging response and data.
const response = [
{
sectionType: 'section1',
test1: 'test 1 data',
test2: '',
test3: 'test 3 data',
},
{
sectionType: 'section3',
test1: 'test 1 data',
test2: 'test 2 data',
test4: 'test 4 data',
},
{
sectionType: 'section2',
test1: 'test 1 data',
test2: 'test 2 data',
test5: 'test 5 data',
},
{
sectionType: 'section1',
test1: 'test 1 data',
test6: '',
test3: 'test 3 data',
},
{
sectionType: 'section3',
test1: '',
test2: 'test 2 data',
test3: 'test 3 data',
},
];
const 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 expectedData = [
{
test1: { value: 'test 1 data', label: 'Test 1' },
test2: { value: '', label: 'Test 2' },
test3: { value: 'test 3 data', label: 'Test 3' },
},
{
test1: { value: 'test 1 data', label: 'Test 1' },
test2: { value: 'test 2 data', label: 'Test 2' },
test4: { value: 'test 4 data', label: 'Test 4' },
},
{
test1: { value: 'test 1 data', label: 'Test 1' },
test2: { value: 'test 2 data', label: 'Test 2' },
test5: { value: 'test 5 data', label: 'Test 5' },
},
{
test1: { value: 'test 1 data', label: 'Test 1' },
test6: { value: '', label: 'Test 6' },
test3: { value: 'test 3 data', label: 'Test 3' },
},
{
test1: { value: '', label: 'Test 1' },
test2: { value: 'test 2 data', label: 'Test 2' },
test3: { value: 'test 3 data', label: 'Test 3' },
},
];
I tried with below code, but getting the object but not completely. Is there any better way to achieve this.
let keysCollection = []
response.forEach(d => {
let keys = Object.keys(d);
keysCollection.push(keys)
})
let mergingKeysCollection = keysCollection.reduce((a,b) => [...a, ...b], [])
let uniqueKeys = Array.from(new Set(mergingKeysCollection))
let actualData = Object.keys(data)