1

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)
1
  • 2
    Try to add less example data. Commented May 24, 2019 at 8:41

1 Answer 1

1

You could take a Map for the labels and map the response by creating new object with the values and the label.

var 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' }],
    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' }] }] }] } },
    labels = Object.entries(data).reduce((l, [k, { forms }]) => {
        forms.forEach(({ fields }) =>
            fields.forEach(({ columnName, label: { 0: { actualLabel } } }) =>
                l.set([k, columnName].join('|'), actualLabel)
            )
        );
        return l;
    }, new Map),
    result = response.map(({ sectionType, ...rest }) =>
        Object.assign({}, ...Object.entries(rest).map(([k, value]) =>
            ({ [k]: { value, label: labels.get([sectionType, k].join('|')) } }))
        )
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

i think the structure in answer is wrong, not getting the result when used with the question data and response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.